1
1
import { FileSystemRouter , type BunFile } from "bun" ;
2
2
import { NJSON } from "next-json" ;
3
3
import { readFileSync , statSync } from "node:fs" ;
4
- import { join , relative , normalize } from "node:path" ;
4
+ import { join , parse , relative } from "node:path" ;
5
5
import { renderToReadableStream } from "react-dom/server" ;
6
6
import { ClientOnlyError } from "./client" ;
7
7
import { MetaContext , PreloadModule } from "./preload" ;
@@ -62,17 +62,24 @@ export class StaticRouters {
62
62
const parsed = require ( metafile ) ;
63
63
this . #hashed = parsed . hashed ;
64
64
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
+ }
72
80
this . #routes_dump = NJSON . stringify ( Object . fromEntries ( this . #routes) , {
73
81
omitStack : true ,
74
82
} ) ;
75
- this . #static_cache. reset ( ) ;
76
83
}
77
84
78
85
async serve < T = void > (
@@ -164,7 +171,7 @@ export class StaticRouters {
164
171
. join ( ";" ) ,
165
172
bootstrapModules : bootstrapModules ?. map ( ( name ) => {
166
173
const hash = this . #hashed[ name ] ;
167
- if ( hash ) return ` ${ name } ? ${ hash } ` ;
174
+ if ( hash ) return hashremap ( name , hash ) ;
168
175
return name ;
169
176
} ) ,
170
177
onError,
@@ -198,7 +205,7 @@ function* scanCacheDependencies(
198
205
: target . endsWith ( ".ts" )
199
206
? "ts"
200
207
: "jsx" ,
201
- } ) . scanImports ( readFileSync ( target ) ) ;
208
+ } ) . scanImports ( readFileSync ( target , "utf-8" ) ) ;
202
209
for ( const imp of imports ) {
203
210
if ( imp . kind === "import-statement" ) {
204
211
const path = require . resolve (
@@ -218,12 +225,20 @@ function* scanCacheDependencies(
218
225
} catch { }
219
226
}
220
227
228
+ function hashremap ( input : string , hash : string ) {
229
+ const parsed = parse ( input ) ;
230
+ return `${ join ( parsed . dir , parsed . name ) } -${ hash } ${ parsed . ext } ` ;
231
+ }
232
+
221
233
export class StaticFileCache {
222
234
#cache = new Map < string , BunFile > ( ) ;
223
235
constructor ( public base : string ) { }
224
236
reset ( ) {
225
237
this . #cache. clear ( ) ;
226
238
}
239
+ remap ( pathname : string , real : string ) {
240
+ this . #cache. set ( pathname , Bun . file ( real ) ) ;
241
+ }
227
242
match ( pathname : string ) : BunFile | undefined {
228
243
if ( this . #cache. has ( pathname ) ) {
229
244
return this . #cache. get ( pathname ) ! ;
0 commit comments