Skip to content

Commit a8f0372

Browse files
Elias-Chairiematipico
andauthoredMay 16, 2024··
Update generator.ts to allow %23 (#) in dynamic urls (#10965)
* Update generator.ts to allow %23 (#) in dynamic urls * added changeset * fix: Update generator.ts to santize url params as well * fix: sanitizeParams function * removed old fix * fix: added test for decoded # and ? * fix: formatting of file * sperated sanitizing of generated paths and ssr dynamic paths * refactor: using map instead * Update .changeset/mean-geckos-sell.md Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * doc: added JSDoc for sanitizeParams function --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent 1df24a4 commit a8f0372

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed
 

‎.changeset/mean-geckos-sell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Update generator.ts to allow %23 (#) in dynamic urls

‎packages/astro/src/core/routing/manifest/generator.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ import type { AstroConfig, RoutePart } from '../../../@types/astro.js';
22

33
import { compile } from 'path-to-regexp';
44

5+
/**
6+
* Sanitizes the parameters object by normalizing string values and replacing certain characters with their URL-encoded equivalents.
7+
* @param {Record<string, string | number | undefined>} params - The parameters object to be sanitized.
8+
* @returns {Record<string, string | number | undefined>} The sanitized parameters object.
9+
*/
10+
function sanitizeParams(params: Record<string, string | number | undefined>): Record<string, string | number | undefined> {
11+
return Object.fromEntries(
12+
Object.entries(params).map(([key, value]) => {
13+
if (typeof value === 'string') {
14+
return [key, value
15+
.normalize()
16+
.replace(/#/g, '%23')
17+
.replace(/\?/g, '%3F')]
18+
}
19+
return [key, value];
20+
})
21+
)
22+
}
23+
524
export function getRouteGenerator(
625
segments: RoutePart[][],
726
addTrailingSlash: AstroConfig['trailingSlash']
@@ -37,8 +56,9 @@ export function getRouteGenerator(
3756
trailing = '/';
3857
}
3958
const toPath = compile(template + trailing);
40-
return (params: object): string => {
41-
const path = toPath(params);
59+
return (params: Record<string, string | number | undefined>): string => {
60+
const sanitizedParams = sanitizeParams(params);
61+
const path = toPath(sanitizedParams);
4262

4363
// When generating an index from a rest parameter route, `path-to-regexp` will return an
4464
// empty string instead "/". This causes an inconsistency with static indexes that may result

‎packages/astro/test/ssr-params.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ describe('Astro.params in SSR', () => {
4242

4343
it('No double URL decoding', async () => {
4444
const app = await fixture.loadTestAdapterApp();
45-
const request = new Request('http://example.com/users/houston/%25');
45+
const request = new Request('http://example.com/users/houston/%25%23%3F');
4646
const response = await app.render(request);
4747
assert.equal(response.status, 200);
4848
const html = await response.text();
4949
const $ = cheerio.load(html);
50-
assert.equal($('.category').text(), '%');
50+
assert.equal($('.category').text(), '%#?');
5151
});
5252
});

0 commit comments

Comments
 (0)
Please sign in to comment.