11import  {  FileSystemRouter ,  type  BunFile  }  from  "bun" ; 
22import  {  NJSON  }  from  "next-json" ; 
33import  {  readFileSync ,  statSync  }  from  "node:fs" ; 
4- import  {  join ,  relative ,   normalize  }  from  "node:path" ; 
4+ import  {  join ,  parse ,   relative  }  from  "node:path" ; 
55import  {  renderToReadableStream  }  from  "react-dom/server" ; 
66import  {  ClientOnlyError  }  from  "./client" ; 
77import  {  MetaContext ,  PreloadModule  }  from  "./preload" ; 
@@ -62,17 +62,24 @@ export class StaticRouters {
6262    const  parsed  =  require ( metafile ) ; 
6363    this . #hashed =  parsed . hashed ; 
6464    this . #dependencies =  parsed . dependencies ; 
65-     this . #routes =  new  Map ( 
66-       Object . entries ( this . client . routes ) . map ( ( [ path ,  filePath ] )  =>  { 
67-         let  target  =  "/"  +  relative ( join ( baseDir ,  buildDir ) ,  filePath ) ; 
68-         if  ( this . #hashed[ target ] )  target  +=  `?${ this . #hashed[ target ] }  ` ; 
69-         return  [ path ,  target ] ; 
70-       } ) 
71-     ) ; 
65+     this . #routes =  new  Map < string ,  string > ( ) ; 
66+     this . #static_cache. reset ( ) ; 
67+     for  ( const  [ path ,  filePath ]  of  Object . entries ( this . client . routes ) )  { 
68+       const  target  =  "/"  +  relative ( join ( baseDir ,  buildDir ) ,  filePath ) ; 
69+       const  converted  =  this . #hashed[ target ] 
70+         ? hashremap ( target ,  this . #hashed[ target ] ) 
71+         : target ; 
72+       this . #routes. set ( path ,  converted ) ; 
73+     } 
74+     for  ( const  [ file ,  hash ]  of  Object . entries ( this . #hashed) )  { 
75+       this . #static_cache. remap ( 
76+         hashremap ( file ,  hash ) , 
77+         join ( baseDir ,  buildDir ,  file ) 
78+       ) ; 
79+     } 
7280    this . #routes_dump =  NJSON . stringify ( Object . fromEntries ( this . #routes) ,  { 
7381      omitStack : true , 
7482    } ) ; 
75-     this . #static_cache. reset ( ) ; 
7683  } 
7784
7885  async  serve < T  =  void > ( 
@@ -164,7 +171,7 @@ export class StaticRouters {
164171          . join ( ";" ) , 
165172        bootstrapModules : bootstrapModules ?. map ( ( name )  =>  { 
166173          const  hash  =  this . #hashed[ name ] ; 
167-           if  ( hash )  return  ` ${ name } ? ${ hash } ` ; 
174+           if  ( hash )  return  hashremap ( name ,   hash ) ; 
168175          return  name ; 
169176        } ) , 
170177        onError, 
@@ -198,7 +205,7 @@ function* scanCacheDependencies(
198205        : target . endsWith ( ".ts" ) 
199206        ? "ts" 
200207        : "jsx" , 
201-     } ) . scanImports ( readFileSync ( target ) ) ; 
208+     } ) . scanImports ( readFileSync ( target ,   "utf-8" ) ) ; 
202209    for  ( const  imp  of  imports )  { 
203210      if  ( imp . kind  ===  "import-statement" )  { 
204211        const  path  =  require . resolve ( 
@@ -218,12 +225,20 @@ function* scanCacheDependencies(
218225  }  catch  { } 
219226} 
220227
228+ function  hashremap ( input : string ,  hash : string )  { 
229+   const  parsed  =  parse ( input ) ; 
230+   return  `${ join ( parsed . dir ,  parsed . name ) }  -${ hash } ${ parsed . ext }  ` ; 
231+ } 
232+ 
221233export  class  StaticFileCache  { 
222234  #cache =  new  Map < string ,  BunFile > ( ) ; 
223235  constructor ( public  base : string )  { } 
224236  reset ( )  { 
225237    this . #cache. clear ( ) ; 
226238  } 
239+   remap ( pathname : string ,  real : string )  { 
240+     this . #cache. set ( pathname ,  Bun . file ( real ) ) ; 
241+   } 
227242  match ( pathname : string ) : BunFile  |  undefined  { 
228243    if  ( this . #cache. has ( pathname ) )  { 
229244      return  this . #cache. get ( pathname ) ! ; 
0 commit comments