Skip to content

Commit

Permalink
Remove dependency on path-to-regexp (#11983)
Browse files Browse the repository at this point in the history
Fixes #11956
Closes #11985
Closes 11965
  • Loading branch information
uwej711 committed Sep 13, 2024
1 parent c9ae7b1 commit 633eeaa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-lemons-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Remove dependency on path-to-regexp
1 change: 0 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@
"ora": "^8.1.0",
"p-limit": "^6.1.0",
"p-queue": "^8.0.1",
"path-to-regexp": "^6.2.2",
"preferred-pm": "^4.0.0",
"prompts": "^2.4.2",
"rehype": "^13.0.1",
Expand Down
68 changes: 30 additions & 38 deletions packages/astro/src/core/routing/manifest/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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.
Expand All @@ -24,45 +22,39 @@ export function getRouteGenerator(
segments: RoutePart[][],
addTrailingSlash: AstroConfig['trailingSlash'],
) {
const template = segments
.map((segment) => {
return (
'/' +
segment
.map((part) => {
if (part.spread) {
return `:${part.content.slice(3)}(.*)?`;
} else if (part.dynamic) {
return `:${part.content}`;
} else {
return part.content
.normalize()
.replace(/\?/g, '%3F')
.replace(/#/g, '%23')
.replace(/%5B/g, '[')
.replace(/%5D/g, ']')
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
})
.join('')
);
})
.join('');

// Unless trailingSlash config is set to 'always', don't automatically append it.
let trailing: '/' | '' = '';
if (addTrailingSlash === 'always' && segments.length) {
trailing = '/';
}
const toPath = compile(template + trailing);
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
// in the incorrect routes being rendered.
// To fix this, we return "/" when the path is empty.
// Unless trailingSlash config is set to 'always', don't automatically append it.
let trailing: '/' | '' = '';
if (addTrailingSlash === 'always' && segments.length) {
trailing = '/';
}

const path = segments
.map((segment) => {
return (
'/' +
segment
.map((part) => {
if (part.spread) {
return `${sanitizedParams[part.content.slice(3)] || ''}`;
} else if (part.dynamic) {
return `${sanitizedParams[part.content] || ''}`;
} else {
return part.content
.normalize()
.replace(/\?/g, '%3F')
.replace(/#/g, '%23')
.replace(/%5B/g, '[')
.replace(/%5D/g, ']');
}
})
.join('')
);
})
.join('') + trailing;

return path || '/';
};
}
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 633eeaa

Please sign in to comment.