diff --git a/src/server/router.ts b/src/server/router.ts index c9c1ce1e..76bb3698 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -50,7 +50,7 @@ export type DestinationKind = "internal" | "static" | "route" | "notFound"; // deno-lint-ignore ban-types export type InternalRoute = { baseRoute: BaseRoute; - pattern: URLPattern; + pattern: URLPattern | string; methods: { [K in KnownMethod]?: MatchHandler }; default?: MatchHandler; destination: DestinationKind; @@ -109,14 +109,16 @@ export function defaultUnknownMethodHandler( } function processRoutes( - processedRoutes: InternalRoute[], + processedRoutes: Array | null>, routes: Routes, destination: DestinationKind, ) { for (const [path, def] of Object.entries(routes)) { const entry: InternalRoute = { 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( } } +interface RouteResult { + route: InternalRoute | undefined; + params: Record; +} + export function getParamsAndRoute( { internalRoutes, @@ -142,13 +149,39 @@ export function getParamsAndRoute( }: RouterOptions, ): ( url: string, -) => { route: InternalRoute | undefined; params: Record } { - const processedRoutes: InternalRoute[] = []; +) => RouteResult { + const processedRoutes: Array | null> = []; processRoutes(processedRoutes, internalRoutes, "internal"); processRoutes(processedRoutes, staticRoutes, "static"); processRoutes(processedRoutes, routes, "route"); + + const statics = new Map>(); + 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( 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 {