summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Emanuele Stoppa <my.burning@gmail.com> 2024-10-30 11:45:42 +0000
committerGravatar GitHub <noreply@github.com> 2024-10-30 11:45:42 +0000
commit25192a059975f5a31a9c43e5d605541f4e9618bc (patch)
tree1a98990362a06e818732d1a01229a918ed91b194
parent5376acf0827a81afe21d540dac4129434b4ecf71 (diff)
downloadastro-25192a059975f5a31a9c43e5d605541f4e9618bc.tar.gz
astro-25192a059975f5a31a9c43e5d605541f4e9618bc.tar.zst
astro-25192a059975f5a31a9c43e5d605541f4e9618bc.zip
fix(routing): match against decoded pathname (#12270)
-rw-r--r--.changeset/metal-birds-admire.md5
-rw-r--r--packages/astro/src/core/render/params-and-props.ts5
-rw-r--r--packages/astro/test/fixtures/ssr-params/src/pages/[category].astro1
-rw-r--r--packages/astro/test/params.test.js27
4 files changed, 36 insertions, 2 deletions
diff --git a/.changeset/metal-birds-admire.md b/.changeset/metal-birds-admire.md
new file mode 100644
index 000000000..56ee190d1
--- /dev/null
+++ b/.changeset/metal-birds-admire.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fixes a bug where the params weren't correctly computed when rendering URLs with non-English characters
diff --git a/packages/astro/src/core/render/params-and-props.ts b/packages/astro/src/core/render/params-and-props.ts
index 5eabbc0d4..588969eb4 100644
--- a/packages/astro/src/core/render/params-and-props.ts
+++ b/packages/astro/src/core/render/params-and-props.ts
@@ -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({
diff --git a/packages/astro/test/fixtures/ssr-params/src/pages/[category].astro b/packages/astro/test/fixtures/ssr-params/src/pages/[category].astro
index d5bdfd3a6..2e2ca3a82 100644
--- a/packages/astro/test/fixtures/ssr-params/src/pages/[category].astro
+++ b/packages/astro/test/fixtures/ssr-params/src/pages/[category].astro
@@ -6,6 +6,7 @@ export function getStaticPaths() {
{ params: { category: "%2Fsomething" } },
{ params: { category: "%3Fsomething" } },
{ params: { category: "[page]" } },
+ { params: { category: "你好" } },
]
}
---
diff --git a/packages/astro/test/params.test.js b/packages/astro/test/params.test.js
index 14addbb96..7508b7352 100644
--- a/packages/astro/test/params.test.js
+++ b/packages/astro/test/params.test.js
@@ -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;