fix: speedup slow matching of static routes (#1932)

This commit is contained in:
Marvin Hagemeister
2023-10-13 19:18:34 +02:00
committed by GitHub
parent a7d3fbf8d9
commit fe1e622cf1
+63 -34
View File
@@ -50,7 +50,7 @@ export type DestinationKind = "internal" | "static" | "route" | "notFound";
// deno-lint-ignore ban-types // deno-lint-ignore ban-types
export type InternalRoute<T = {}> = { export type InternalRoute<T = {}> = {
baseRoute: BaseRoute; baseRoute: BaseRoute;
pattern: URLPattern; pattern: URLPattern | string;
methods: { [K in KnownMethod]?: MatchHandler<T> }; methods: { [K in KnownMethod]?: MatchHandler<T> };
default?: MatchHandler<T>; default?: MatchHandler<T>;
destination: DestinationKind; destination: DestinationKind;
@@ -109,14 +109,16 @@ export function defaultUnknownMethodHandler(
} }
function processRoutes<T>( function processRoutes<T>(
processedRoutes: InternalRoute<T>[], processedRoutes: Array<InternalRoute<T> | null>,
routes: Routes<T>, routes: Routes<T>,
destination: DestinationKind, destination: DestinationKind,
) { ) {
for (const [path, def] of Object.entries(routes)) { for (const [path, def] of Object.entries(routes)) {
const entry: InternalRoute<T> = { const entry: InternalRoute<T> = {
baseRoute: def.baseRoute, baseRoute: def.baseRoute,
pattern: new URLPattern({ pathname: path }), pattern: destination === "static"
? path
: new URLPattern({ pathname: path }),
methods: {}, methods: {},
default: undefined, default: undefined,
destination, destination,
@@ -134,6 +136,11 @@ function processRoutes<T>(
} }
} }
interface RouteResult<T> {
route: InternalRoute<T> | undefined;
params: Record<string, string>;
}
export function getParamsAndRoute<T>( export function getParamsAndRoute<T>(
{ {
internalRoutes, internalRoutes,
@@ -142,13 +149,39 @@ export function getParamsAndRoute<T>(
}: RouterOptions<T>, }: RouterOptions<T>,
): ( ): (
url: string, url: string,
) => { route: InternalRoute<T> | undefined; params: Record<string, string> } { ) => RouteResult<T> {
const processedRoutes: InternalRoute<T>[] = []; const processedRoutes: Array<InternalRoute<T> | null> = [];
processRoutes(processedRoutes, internalRoutes, "internal"); processRoutes(processedRoutes, internalRoutes, "internal");
processRoutes(processedRoutes, staticRoutes, "static"); processRoutes(processedRoutes, staticRoutes, "static");
processRoutes(processedRoutes, routes, "route"); processRoutes(processedRoutes, routes, "route");
const statics = new Map<string, RouteResult<T>>();
return (url: string) => { return (url: string) => {
for (const route of processedRoutes) { const pathname = new URL(url).pathname;
const cached = statics.get(pathname);
if (cached !== undefined) {
return cached;
}
for (let i = 0; i < processedRoutes.length; i++) {
const route = processedRoutes[i];
if (route === null) continue;
// Static routes where the full pattern contains no dynamic
// parts and must be an exact match. We use that for static
// files.
if (typeof route.pattern === "string") {
if (route.pattern === pathname) {
processedRoutes[i] = null;
const res = { route: route, params: {} };
statics.set(route.pattern, res);
return res;
}
continue;
}
const res = route.pattern.exec(url); const res = route.pattern.exec(url);
if (res !== null) { if (res !== null) {
@@ -182,40 +215,36 @@ export function router<T = unknown>(
return (req, ctx, groups, route) => { return (req, ctx, groups, route) => {
if (route) { if (route) {
const res = route.pattern.exec(req.url); // If not overridden, HEAD requests should be handled as GET requests but without the body.
if (req.method === "HEAD" && !route.methods["HEAD"]) {
req = new Request(req.url, { method: "GET", headers: req.headers });
}
if (res !== null) { for (const [method, handler] of Object.entries(route.methods)) {
// If not overridden, HEAD requests should be handled as GET requests but without the body. if (req.method === method) {
if (req.method === "HEAD" && !route.methods["HEAD"]) {
req = new Request(req.url, { method: "GET", headers: req.headers });
}
for (const [method, handler] of Object.entries(route.methods)) {
if (req.method === method) {
return {
destination: route.destination,
handler: () => handler(req, ctx, groups),
};
}
}
if (route.default) {
return { return {
destination: route.destination, destination: route.destination,
handler: () => route.default!(req, ctx, groups), handler: () => handler(req, ctx, groups),
};
} else {
return {
destination: route.destination,
handler: () =>
unknownMethodHandler!(
req,
ctx,
Object.keys(route.methods) as KnownMethod[],
),
}; };
} }
} }
if (route.default) {
return {
destination: route.destination,
handler: () => route.default!(req, ctx, groups),
};
} else {
return {
destination: route.destination,
handler: () =>
unknownMethodHandler!(
req,
ctx,
Object.keys(route.methods) as KnownMethod[],
),
};
}
} }
return { return {