diff --git a/.changeset/brave-cats-retire.md b/.changeset/brave-cats-retire.md new file mode 100644 index 000000000000..14ef7ff539e9 --- /dev/null +++ b/.changeset/brave-cats-retire.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes the bug that rewrite will pass encoded url to the dynamic routing and cause params mismatch. diff --git a/packages/astro/src/core/routing/rewrite.ts b/packages/astro/src/core/routing/rewrite.ts index 40f91441316b..78f70e847386 100644 --- a/packages/astro/src/core/routing/rewrite.ts +++ b/packages/astro/src/core/routing/rewrite.ts @@ -53,9 +53,10 @@ export function findRouteToRewrite({ pathname = pathname.slice(base.length); } + const decodedPathname = decodeURI(pathname); let foundRoute; for (const route of routes) { - if (route.pattern.test(decodeURI(pathname))) { + if (route.pattern.test(decodedPathname)) { foundRoute = route; break; } @@ -65,7 +66,7 @@ export function findRouteToRewrite({ return { routeData: foundRoute, newUrl, - pathname, + pathname: decodedPathname, }; } else { const custom404 = routes.find((route) => route.route === '/404'); diff --git a/packages/astro/test/fixtures/rewrite-dynamic-routing/astro.config.mjs b/packages/astro/test/fixtures/rewrite-dynamic-routing/astro.config.mjs new file mode 100644 index 000000000000..c800d0ddaacc --- /dev/null +++ b/packages/astro/test/fixtures/rewrite-dynamic-routing/astro.config.mjs @@ -0,0 +1,6 @@ +import {defineConfig} from 'astro/config'; + +// https://astro.build/config +export default defineConfig({ + site: "https://example.com" +}); diff --git a/packages/astro/test/fixtures/rewrite-dynamic-routing/package.json b/packages/astro/test/fixtures/rewrite-dynamic-routing/package.json new file mode 100644 index 000000000000..16d139d40e6a --- /dev/null +++ b/packages/astro/test/fixtures/rewrite-dynamic-routing/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/rewrite-dynamic-routing", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/[id].astro b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/[id].astro new file mode 100644 index 000000000000..156b686b3a4b --- /dev/null +++ b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/[id].astro @@ -0,0 +1,17 @@ +--- +export function getStaticPaths() { + return [{ params: { id: 'ABC abc 123' } }, + { params: { id: 'test' } }] +} + +const { id } = Astro.params +--- + + + Index + + +

Index

+

{id}

+ + \ No newline at end of file diff --git a/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/bar.astro b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/bar.astro new file mode 100644 index 000000000000..7c8aeb15d9ea --- /dev/null +++ b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/bar.astro @@ -0,0 +1,3 @@ +--- +return Astro.rewrite('/ABC abc 123') +--- \ No newline at end of file diff --git a/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/foo.astro b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/foo.astro new file mode 100644 index 000000000000..6aa1d42036d1 --- /dev/null +++ b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/foo.astro @@ -0,0 +1,3 @@ +--- +return Astro.rewrite('/has space/test') +--- \ No newline at end of file diff --git a/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/has space/[id].astro b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/has space/[id].astro new file mode 100644 index 000000000000..156b686b3a4b --- /dev/null +++ b/packages/astro/test/fixtures/rewrite-dynamic-routing/src/pages/has space/[id].astro @@ -0,0 +1,17 @@ +--- +export function getStaticPaths() { + return [{ params: { id: 'ABC abc 123' } }, + { params: { id: 'test' } }] +} + +const { id } = Astro.params +--- + + + Index + + +

Index

+

{id}

+ + \ No newline at end of file diff --git a/packages/astro/test/rewrite.test.js b/packages/astro/test/rewrite.test.js index cc7508081092..7e508593e8ac 100644 --- a/packages/astro/test/rewrite.test.js +++ b/packages/astro/test/rewrite.test.js @@ -111,6 +111,37 @@ describe('Dev rewrite, trailing slash -> never, with base', () => { }); }); +describe('Dev rewrite, dynamic routing', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + let devServer; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/rewrite-dynamic-routing/', + }); + devServer = await fixture.startDevServer(); + }); + + after(async () => { + await devServer.stop(); + }); + + it('should decode the escaped characters in the URL', async () => { + const html = await fixture.fetch('/foo').then((res) => res.text()); + const $ = cheerioLoad(html); + + assert.equal($('h1').text(), 'Index'); + }); + + it('should decode the escaped characters in the params', async () => { + const html = await fixture.fetch('/bar').then((res) => res.text()); + const $ = cheerioLoad(html); + + assert.equal($('h1').text(), 'Index'); + }); +}); + describe('Dev rewrite, hybrid/server', () => { /** @type {import('./test-utils').Fixture} */ let fixture; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 31a2544cee88..98c07e39a390 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3609,6 +3609,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/rewrite-dynamic-routing: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/rewrite-i18n-manual-routing: dependencies: astro: