Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle pathname for dynamic routing in rewrite #13113

Merged
merged 4 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-cats-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes the bug that rewrite will pass encoded url to the dynamic routing and cause params mismatch.
5 changes: 3 additions & 2 deletions packages/astro/src/core/routing/rewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -65,7 +66,7 @@ export function findRouteToRewrite({
return {
routeData: foundRoute,
newUrl,
pathname,
pathname: decodedPathname,
};
} else {
const custom404 = routes.find((route) => route.route === '/404');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {defineConfig} from 'astro/config';

// https://astro.build/config
export default defineConfig({
site: "https://example.com"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/rewrite-dynamic-routing",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
export function getStaticPaths() {
return [{ params: { id: 'ABC abc 123' } },
{ params: { id: 'test' } }]
}

const { id } = Astro.params
---
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
<p>{id}</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
return Astro.rewrite('/ABC abc 123')
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
return Astro.rewrite('/has space/test')
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
export function getStaticPaths() {
return [{ params: { id: 'ABC abc 123' } },
{ params: { id: 'test' } }]
}

const { id } = Astro.params
---
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
<p>{id}</p>
</body>
</html>
31 changes: 31 additions & 0 deletions packages/astro/test/rewrite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.