From 1692f1e036b951de4b77acfb04f0c89e79a8ca68 Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 27 Dec 2023 23:27:16 +0800 Subject: [PATCH] Prevent double uri decoding --- .changeset/old-goats-occur.md | 5 +++++ packages/astro/src/core/routing/params.ts | 4 ++-- packages/astro/src/vite-plugin-astro-server/request.ts | 2 +- packages/astro/test/ssr-params.test.js | 10 ++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .changeset/old-goats-occur.md diff --git a/.changeset/old-goats-occur.md b/.changeset/old-goats-occur.md new file mode 100644 index 000000000000..8224bbabbd44 --- /dev/null +++ b/.changeset/old-goats-occur.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Prevents unnecessary URI decoding when rendering a route diff --git a/packages/astro/src/core/routing/params.ts b/packages/astro/src/core/routing/params.ts index 987528d5786a..973f7f2b5e99 100644 --- a/packages/astro/src/core/routing/params.ts +++ b/packages/astro/src/core/routing/params.ts @@ -12,9 +12,9 @@ export function getParams(array: string[]) { const params: Params = {}; array.forEach((key, i) => { if (key.startsWith('...')) { - params[key.slice(3)] = match[i + 1] ? decodeURIComponent(match[i + 1]) : undefined; + params[key.slice(3)] = match[i + 1] ? match[i + 1] : undefined; } else { - params[key] = decodeURIComponent(match[i + 1]); + params[key] = match[i + 1]; } }); return params; diff --git a/packages/astro/src/vite-plugin-astro-server/request.ts b/packages/astro/src/vite-plugin-astro-server/request.ts index 5dd507c75b8d..29ceafa0c5ab 100644 --- a/packages/astro/src/vite-plugin-astro-server/request.ts +++ b/packages/astro/src/vite-plugin-astro-server/request.ts @@ -37,7 +37,7 @@ export async function handleRequest({ if (config.trailingSlash === 'never' && !incomingRequest.url) { pathname = ''; } else { - pathname = decodeURI(url.pathname); + pathname = url.pathname; } // Add config.base back to url before passing it to SSR diff --git a/packages/astro/test/ssr-params.test.js b/packages/astro/test/ssr-params.test.js index 343e47cf8705..b15955a9faf4 100644 --- a/packages/astro/test/ssr-params.test.js +++ b/packages/astro/test/ssr-params.test.js @@ -38,4 +38,14 @@ describe('Astro.params in SSR', () => { expect($('.category').text()).to.equal('food'); }); }); + + it('No double URL decoding', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/users/houston/%25'); + 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('%'); + }); });