Skip to content

Commit bbd745b

Browse files
committed
fix: htmlFallbackMiddleware should check fullBundleMode memoryFiles
1 parent 0aa5eb8 commit bbd745b

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

packages/vite/src/node/preview.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ export async function preview(
246246

247247
// html fallback
248248
if (config.appType === 'spa' || config.appType === 'mpa') {
249-
app.use(htmlFallbackMiddleware(distDir, config.appType === 'spa'))
249+
app.use(
250+
htmlFallbackMiddleware(distDir, config.appType === 'spa', false, {}),
251+
)
250252
}
251253

252254
// apply post server hooks from plugins

packages/vite/src/node/server/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,14 @@ export async function _createServer(
939939

940940
// html fallback
941941
if (config.appType === 'spa' || config.appType === 'mpa') {
942-
middlewares.use(htmlFallbackMiddleware(root, config.appType === 'spa'))
942+
middlewares.use(
943+
htmlFallbackMiddleware(
944+
root,
945+
config.appType === 'spa',
946+
!!config.experimental.fullBundleMode,
947+
server.memoryFiles,
948+
),
949+
)
943950
}
944951

945952
// run post config hooks

packages/vite/src/node/server/middlewares/htmlFallback.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const debug = createDebugger('vite:html-fallback')
99
export 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

Comments
 (0)