From 3776ecf0aa9e08a992d3ae76e90682fd04093721 Mon Sep 17 00:00:00 2001 From: Nworm <35765846+1574242600@users.noreply.github.com> Date: Fri, 15 Mar 2024 18:27:07 +0800 Subject: [PATCH] fix(routing): partially dynamic segments are being truncated (#10379) * fix(routing): partially dynamic segments are being truncated and affecting routing. * test: fix "/static should be higher priority than /static-" * Update .changeset/five-colts-rescue.md Co-authored-by: Florian Lefebvre * test: fix the check that was wrongly deleted. * other: improve code readability. --------- Co-authored-by: Florian Lefebvre --- .changeset/five-colts-rescue.md | 5 ++ .../astro/src/core/routing/manifest/create.ts | 20 ++++---- .../astro/test/units/routing/manifest.test.js | 48 ++++++++++++++++++- 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 .changeset/five-colts-rescue.md 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': `

test

`, + '/src/pages/blog/a-[b].233.ts': ``, + }, + root + ); + + const settings = await createBasicSettings({ + root: fileURLToPath(root), + output: 'server', + base: '/search', + trailingSlash: 'never', + redirects: { + '/posts/a-[b].233': '/blog/a-[b].233', + }, + experimental: { + globalRoutePriority: true, + }, + }); + + settings.injectedRoutes = [ + { + pattern: '/[c]-d', + entrypoint: '@lib/legacy/dynamic.astro', + priority: 'normal', + }, + ]; + + const manifest = createRouteManifest({ + cwd: fileURLToPath(root), + settings, + fsMod: fs, + }); + + assert.deepEqual(getManifestRoutes(manifest), [ + { type: 'endpoint', route: '/blog/a-[b].233' }, + { type: 'redirect', route: '/posts/a-[b].233' }, + { type: 'page', route: '/[c]-d' }, + { type: 'page', route: '/a-[b]' }, + ]); + }); });