diff --git a/.changeset/five-colts-rescue.md b/.changeset/five-colts-rescue.md new file mode 100644 index 000000000000..61761796751c --- /dev/null +++ b/.changeset/five-colts-rescue.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes a routing issue with partially truncated dynamic segments. diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index 00b87a049d39..e30440fa4f8d 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -401,9 +401,7 @@ function createFileBasedRoutes( const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic) ? `/${segments.map((segment) => segment[0].content).join('/')}` : null; - const route = `/${segments - .map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content)) - .join('/')}`.toLowerCase(); + const route = joinSegments(segments); routes.push({ route, isIndex: item.isIndex, @@ -478,9 +476,7 @@ function createInjectedRoutes({ settings, cwd }: CreateRouteManifestParams): Pri .flat() .filter((p) => p.dynamic) .map((p) => p.content); - const route = `/${segments - .map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content)) - .join('/')}`.toLowerCase(); + const route = joinSegments(segments); routes[priority].push({ type, @@ -536,9 +532,7 @@ function createRedirectRoutes( .flat() .filter((p) => p.dynamic) .map((p) => p.content); - const route = `/${segments - .map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content)) - .join('/')}`.toLowerCase(); + const route = joinSegments(segments); let destination: string; if (typeof to === 'string') { @@ -899,3 +893,11 @@ function computeRoutePriority(config: AstroConfig): RoutePriorityOverride { } return 'legacy'; } + +function joinSegments(segments: RoutePart[][]): string { + const arr = segments.map((segment) => { + return segment.map((rp) => (rp.dynamic ? `[${rp.content}]` : rp.content)).join(''); + }); + + return `/${arr.join('/')}`.toLowerCase(); +} diff --git a/packages/astro/test/units/routing/manifest.test.js b/packages/astro/test/units/routing/manifest.test.js index 91816d1bc670..e5fa35729938 100644 --- a/packages/astro/test/units/routing/manifest.test.js +++ b/packages/astro/test/units/routing/manifest.test.js @@ -144,8 +144,8 @@ describe('routing - createRouteManifest', () => { assertRouteRelations(getManifestRoutes(manifest), [ ['/', '/[...rest]'], - ['/static', '/static-'], - ['/static-', '/[dynamic]'], + ['/static', '/static-[dynamic]'], + ['/static-[dynamic]', '/[dynamic]'], ['/static', '/[dynamic]'], ['/static', '/[...rest]'], ['/[dynamic]', '/[...rest]'], @@ -567,4 +567,48 @@ describe('routing - createRouteManifest', () => { }, ]); }); + + it('should concatenate each part of the segment. issues#10122', async () => { + const fs = createFs( + { + '/src/pages/a-[b].astro': `