@@ -6,7 +6,7 @@ import { join } from "pathe";
66import { runtimeDir } from "nitro/meta" ;
77import { addRoute , createRouter , findRoute , findAllRoutes } from "rou3" ;
88import { compileRouterToString } from "rou3/compiler" ;
9- import { hash } from "ohash " ;
9+ import { hash } from "./utils/hash.ts " ;
1010
1111const isGlobalMiddleware = ( h : NitroEventHandler ) => ! h . method && ( ! h . route || h . route === "/**" ) ;
1212
@@ -122,7 +122,7 @@ export function initNitroRouting(nitro: Nitro) {
122122}
123123
124124function handlerWithImportHash ( h : NitroEventHandler ) {
125- const id = ( h . lazy ? "_lazy_" : "_" ) + hash ( h . handler ) . replace ( / - / g , "" ) . slice ( 0 , 6 ) ;
125+ const id = ( h . lazy ? "_lazy_" : "_" ) + hash ( h . handler ) ;
126126 return { ...h , _importHash : id } ;
127127}
128128
@@ -137,7 +137,15 @@ export interface Route<T = unknown> {
137137export class Router < T > {
138138 _routes ?: Route < T > [ ] ;
139139 _router ?: RouterContext < T > ;
140- _compiled ?: Record < string , string > ;
140+ /**
141+ * Cached output of {@link compileToString}, invalidated by {@link _update}.
142+ *
143+ * Only one result is cached: each Router instance must always be compiled with
144+ * the same `opts`. Compiling one instance with differing `opts` would silently
145+ * return the first result, since `opts` holds a `serialize` closure and is
146+ * therefore not hashable into a cache key.
147+ */
148+ _compiled ?: string ;
141149 _baseURL : string ;
142150
143151 constructor ( baseURL ?: string ) {
@@ -169,12 +177,10 @@ export class Router<T> {
169177 }
170178
171179 compileToString ( opts ?: RouterCompilerOptions < T > ) {
172- const key = opts ? hash ( opts ) : "" ;
173- this . _compiled ||= { } ;
174- if ( this . _compiled [ key ] ) {
175- return this . _compiled [ key ] ;
180+ if ( this . _compiled ) {
181+ return this . _compiled ;
176182 }
177- this . _compiled [ key ] = compileRouterToString ( this . _router ! , undefined , opts ) ;
183+ this . _compiled = compileRouterToString ( this . _router ! , undefined , opts ) ;
178184
179185 // TODO: Upstream to rou3 compiler
180186 const onlyWildcard =
@@ -190,11 +196,10 @@ export class Router<T> {
190196 const guardCode = base
191197 ? `if(p!==${ JSON . stringify ( base ) } &&!p.startsWith(${ JSON . stringify ( base + "/" ) } )){return ${ opts ?. matchAll ? "[]" : "undefined" } ;}`
192198 : "" ;
193- this . _compiled [ key ] =
194- /* js */ `/* @__PURE__ */ (() => {const data=${ data } ;return ((_m, p)=>{${ guardCode } return ${ retCode } ;})})()` ;
199+ this . _compiled = /* js */ `/* @__PURE__ */ (() => {const data=${ data } ;return ((_m, p)=>{${ guardCode } return ${ retCode } ;})})()` ;
195200 }
196201
197- return this . _compiled [ key ] ;
202+ return this . _compiled ;
198203 }
199204
200205 match ( method : string , path : string ) : undefined | T {
0 commit comments