Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions website/handlers/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const rankRoute = (pattern: string): number =>
pattern
.split("/")
.reduce((acc, routePart) => {
if (routePart.startsWith("}?")) {
if (routePart.includes("*")) {
return acc - 2;
}
return acc - 1;
}
if (routePart === "*") {
return acc;
}
Expand Down Expand Up @@ -79,17 +85,20 @@ export const router = (
}
for (const { pathTemplate: routePath, handler } of routes) {
const pattern = urlPatternCache[routePath] ??= (() => {
let url;
if (URL.canParse(routePath)) {
url = new URL(routePath);
} else {
url = new URL(routePath, "http://localhost:8000");
return new URLPattern(routePath);
}
const patternWithDefaultOrigin = new URLPattern(
routePath,
"http://localhost:8000",
);

return new URLPattern({
pathname: url.pathname,
...(url.search ? { search: url.search } : {}),
pathname: patternWithDefaultOrigin.pathname,
search: patternWithDefaultOrigin.search || "*",
});
})();

const res = pattern.exec(req.url);
const groups = res?.pathname.groups ?? {};
if (res !== null) {
Expand Down Expand Up @@ -151,6 +160,7 @@ const prepareRoutes = (audiences: Routes[], ctx: AppContext) => {
((highPriorityA ? HIGH_PRIORITY_ROUTE_RANK_BASE_VALUE : 0) +
rankRoute(routeStringA))
);

return {
routes: builtRoutes.map((route) => ({
pathTemplate: route[0],
Expand Down
Loading