Skip to content

Commit

Permalink
fix: correctly handle pathname for dynamic routing in rewrite (#13113)
Browse files Browse the repository at this point in the history
  • Loading branch information
unprintable123 authored Jan 31, 2025
1 parent c33eccb commit 3a26e45
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 2 deletions.
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.

0 comments on commit 3a26e45

Please sign in to comment.