Fix Astro.params does not contain path parameter from URL with non-English characters (#6859)

This commit is contained in:
André Alves 2023-04-17 11:44:48 -03:00 committed by GitHub
parent 1ed52c5849
commit 4c7ba4da08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix Astro.params does not contain path parameter from URL with non-English characters.

View file

@ -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);

View file

@ -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>

View file

@ -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');
});
});
});