@@ -9,6 +9,8 @@ const debug = createDebugger('vite:html-fallback')
99export function htmlFallbackMiddleware (
1010 root : string ,
1111 spaFallback : boolean ,
12+ fullBundleMode : boolean ,
13+ memoryFiles : Record < string , string | Uint8Array > ,
1214) : Connect . NextHandleFunction {
1315 // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
1416 return function viteHtmlFallbackMiddleware ( req , _res , next ) {
@@ -31,6 +33,12 @@ export function htmlFallbackMiddleware(
3133 const url = cleanUrl ( req . url ! )
3234 const pathname = decodeURIComponent ( url )
3335
36+ function checkFileExists ( htmlPathName : string ) {
37+ return fullBundleMode
38+ ? memoryFiles [ htmlPathName ]
39+ : fs . existsSync ( path . join ( root , htmlPathName ) )
40+ }
41+
3442 // .html files are not handled by serveStaticMiddleware
3543 // so we need to check if the file exists
3644 if ( pathname . endsWith ( '.html' ) ) {
@@ -43,8 +51,7 @@ export function htmlFallbackMiddleware(
4351 }
4452 // trailing slash should check for fallback index.html
4553 else if ( pathname . endsWith ( '/' ) ) {
46- const filePath = path . join ( root , pathname , 'index.html' )
47- if ( fs . existsSync ( filePath ) ) {
54+ if ( checkFileExists ( path . join ( pathname , 'index.html' ) ) ) {
4855 const newUrl = url + 'index.html'
4956 debug ?.( `Rewriting ${ req . method } ${ req . url } to ${ newUrl } ` )
5057 req . url = newUrl
@@ -53,8 +60,7 @@ export function htmlFallbackMiddleware(
5360 }
5461 // non-trailing slash should check for fallback .html
5562 else {
56- const filePath = path . join ( root , pathname + '.html' )
57- if ( fs . existsSync ( filePath ) ) {
63+ if ( checkFileExists ( pathname + '.html' ) ) {
5864 const newUrl = url + '.html'
5965 debug ?.( `Rewriting ${ req . method } ${ req . url } to ${ newUrl } ` )
6066 req . url = newUrl
0 commit comments