0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(routing): match against decoded pathname (#12270)

This commit is contained in:
Emanuele Stoppa 2024-10-30 11:45:42 +00:00 committed by GitHub
parent 5376acf082
commit 25192a0599
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a bug where the params weren't correctly computed when rendering URLs with non-English characters

View file

@ -47,7 +47,10 @@ export async function getProps(opts: GetParamsAndPropsOptions): Promise<Props> {
base,
});
const params = getParams(route, pathname);
// The pathname used here comes from the server, which already encored.
// Since we decided to not mess up with encoding anymore, we need to decode them back so the parameters can match
// the ones expected from the users
const params = getParams(route, decodeURI(pathname));
const matchedStaticPath = findPathItemByKey(staticPaths, params, route, logger);
if (!matchedStaticPath && (serverLike ? route.prerender : true)) {
throw new AstroError({

View file

@ -6,6 +6,7 @@ export function getStaticPaths() {
{ params: { category: "%2Fsomething" } },
{ params: { category: "%3Fsomething" } },
{ params: { category: "[page]" } },
{ params: { category: "你好" } },
]
}
---

View file

@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
@ -90,6 +90,31 @@ describe('Astro.params in SSR', () => {
});
});
describe('Astro.params in dev mode', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-params/',
adapter: testAdapter(),
output: 'server',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('should handle non-english URLs', async () => {
const html = await fixture.fetch('/你好').then((res) => res.text());
const $ = cheerio.load(html);
assert.equal($('.category').text(), '你好');
});
});
describe('Astro.params in static mode', () => {
let fixture;