Skip to content

Commit

Permalink
Fix Astro.params does not contain path parameter from URL with non-En…
Browse files Browse the repository at this point in the history
…glish characters (#6859)
  • Loading branch information
andremralves authored Apr 17, 2023
1 parent 1ed52c5 commit 4c7ba4d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/few-cats-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Astro.params does not contain path parameter from URL with non-English characters.
4 changes: 3 additions & 1 deletion packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export async function getParamsAndProps(
let pageProps: Props;
if (route && !route.pathname) {
if (route.params.length) {
const paramsMatch = route.pattern.exec(pathname);
// The RegExp pattern expects a decoded string, but the pathname is encoded
// when the URL contains non-English characters.
const paramsMatch = route.pattern.exec(decodeURIComponent(pathname));
if (paramsMatch) {
params = getParams(route.params)(paramsMatch);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
const { category } = Astro.params
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<h2 class="category">{ category }</h2>
</body>
</html>
12 changes: 12 additions & 0 deletions packages/astro/test/ssr-params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ describe('Astro.params in SSR', () => {
const $ = cheerio.load(html);
expect($('.category').text()).to.equal('food');
});

describe('Non-english characters in the URL', () => {
it('Params are passed to component', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/users/houston/東西/food');
const response = await app.render(request);
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
expect($('.category').text()).to.equal('food');
});
});
});

0 comments on commit 4c7ba4d

Please sign in to comment.