fix: speedup slow matching of static routes (#1932)
This commit is contained in:
+63
-34
@@ -50,7 +50,7 @@ export type DestinationKind = "internal" | "static" | "route" | "notFound";
|
||||
// deno-lint-ignore ban-types
|
||||
export type InternalRoute<T = {}> = {
|
||||
baseRoute: BaseRoute;
|
||||
pattern: URLPattern;
|
||||
pattern: URLPattern | string;
|
||||
methods: { [K in KnownMethod]?: MatchHandler<T> };
|
||||
default?: MatchHandler<T>;
|
||||
destination: DestinationKind;
|
||||
@@ -109,14 +109,16 @@ export function defaultUnknownMethodHandler(
|
||||
}
|
||||
|
||||
function processRoutes<T>(
|
||||
processedRoutes: InternalRoute<T>[],
|
||||
processedRoutes: Array<InternalRoute<T> | null>,
|
||||
routes: Routes<T>,
|
||||
destination: DestinationKind,
|
||||
) {
|
||||
for (const [path, def] of Object.entries(routes)) {
|
||||
const entry: InternalRoute<T> = {
|
||||
baseRoute: def.baseRoute,
|
||||
pattern: new URLPattern({ pathname: path }),
|
||||
pattern: destination === "static"
|
||||
? path
|
||||
: new URLPattern({ pathname: path }),
|
||||
methods: {},
|
||||
default: undefined,
|
||||
destination,
|
||||
@@ -134,6 +136,11 @@ function processRoutes<T>(
|
||||
}
|
||||
}
|
||||
|
||||
interface RouteResult<T> {
|
||||
route: InternalRoute<T> | undefined;
|
||||
params: Record<string, string>;
|
||||
}
|
||||
|
||||
export function getParamsAndRoute<T>(
|
||||
{
|
||||
internalRoutes,
|
||||
@@ -142,13 +149,39 @@ export function getParamsAndRoute<T>(
|
||||
}: RouterOptions<T>,
|
||||
): (
|
||||
url: string,
|
||||
) => { route: InternalRoute<T> | undefined; params: Record<string, string> } {
|
||||
const processedRoutes: InternalRoute<T>[] = [];
|
||||
) => RouteResult<T> {
|
||||
const processedRoutes: Array<InternalRoute<T> | null> = [];
|
||||
processRoutes(processedRoutes, internalRoutes, "internal");
|
||||
processRoutes(processedRoutes, staticRoutes, "static");
|
||||
processRoutes(processedRoutes, routes, "route");
|
||||
|
||||
const statics = new Map<string, RouteResult<T>>();
|
||||
|
||||
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);
|
||||
|
||||
if (res !== null) {
|
||||
@@ -182,40 +215,36 @@ export function router<T = unknown>(
|
||||
|
||||
return (req, ctx, groups, 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) {
|
||||
// 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 });
|
||||
}
|
||||
|
||||
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) {
|
||||
for (const [method, handler] of Object.entries(route.methods)) {
|
||||
if (req.method === method) {
|
||||
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[],
|
||||
),
|
||||
handler: () => handler(req, ctx, groups),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user