mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
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>
This commit is contained in:
parent
1df24a4a78
commit
a8f0372ea7
3 changed files with 29 additions and 4 deletions
5
.changeset/mean-geckos-sell.md
Normal file
5
.changeset/mean-geckos-sell.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Update generator.ts to allow %23 (#) in dynamic urls
|
|
@ -2,6 +2,25 @@ import type { AstroConfig, RoutePart } from '../../../@types/astro.js';
|
|||
|
||||
import { compile } from 'path-to-regexp';
|
||||
|
||||
/**
|
||||
* Sanitizes the parameters object by normalizing string values and replacing certain characters with their URL-encoded equivalents.
|
||||
* @param {Record<string, string | number | undefined>} params - The parameters object to be sanitized.
|
||||
* @returns {Record<string, string | number | undefined>} The sanitized parameters object.
|
||||
*/
|
||||
function sanitizeParams(params: Record<string, string | number | undefined>): Record<string, string | number | undefined> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(params).map(([key, value]) => {
|
||||
if (typeof value === 'string') {
|
||||
return [key, value
|
||||
.normalize()
|
||||
.replace(/#/g, '%23')
|
||||
.replace(/\?/g, '%3F')]
|
||||
}
|
||||
return [key, value];
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export function getRouteGenerator(
|
||||
segments: RoutePart[][],
|
||||
addTrailingSlash: AstroConfig['trailingSlash']
|
||||
|
@ -37,8 +56,9 @@ export function getRouteGenerator(
|
|||
trailing = '/';
|
||||
}
|
||||
const toPath = compile(template + trailing);
|
||||
return (params: object): string => {
|
||||
const path = toPath(params);
|
||||
return (params: Record<string, string | number | undefined>): string => {
|
||||
const sanitizedParams = sanitizeParams(params);
|
||||
const path = toPath(sanitizedParams);
|
||||
|
||||
// When generating an index from a rest parameter route, `path-to-regexp` will return an
|
||||
// empty string instead "/". This causes an inconsistency with static indexes that may result
|
||||
|
|
|
@ -42,11 +42,11 @@ describe('Astro.params in SSR', () => {
|
|||
|
||||
it('No double URL decoding', async () => {
|
||||
const app = await fixture.loadTestAdapterApp();
|
||||
const request = new Request('http://example.com/users/houston/%25');
|
||||
const request = new Request('http://example.com/users/houston/%25%23%3F');
|
||||
const response = await app.render(request);
|
||||
assert.equal(response.status, 200);
|
||||
const html = await response.text();
|
||||
const $ = cheerio.load(html);
|
||||
assert.equal($('.category').text(), '%');
|
||||
assert.equal($('.category').text(), '%#?');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue