Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/vite/src/node/server/middlewares/htmlFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,20 @@ export function htmlFallbackMiddleware(
}

if (spaFallback) {
debug?.(`Rewriting ${req.method} ${req.url} to /index.html`)
req.url = '/index.html'
// remove a path component at a time from pathname and check for
// the presence of a corresponding index.html
let workingPathname = pathname
while (true) {
const filePath = path.join(root, workingPathname, 'index.html')
if (fs.existsSync(filePath)) {
const newUrl = workingPathname + '/index.html'
debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`)
req.url = newUrl
return next()
}
if (workingPathname === '/') break
workingPathname = path.dirname(workingPathname)
}
}

next()
Expand Down
Loading