Skip to content

Commit 0b105fc

Browse files
committed
Make SPA fallback try each directory component
SPA fallback tries looking for a corresponding index.html for each directory component all the way down to root.
1 parent bcc3144 commit 0b105fc

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ export function htmlFallbackMiddleware(
6363
}
6464

6565
if (spaFallback) {
66-
debug?.(`Rewriting ${req.method} ${req.url} to /index.html`)
67-
req.url = '/index.html'
66+
// remove a path component at a time from pathname and check for
67+
// the presence of a corresponding index.html
68+
let workingPathname = pathname
69+
while (true) {
70+
const filePath = path.join(root, workingPathname, 'index.html')
71+
if (fs.existsSync(filePath)) {
72+
const newUrl = workingPathname + '/index.html'
73+
debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`)
74+
req.url = newUrl
75+
return next()
76+
}
77+
if (workingPathname === '/') break
78+
workingPathname = path.dirname(workingPathname)
79+
}
6880
}
6981

7082
next()

0 commit comments

Comments
 (0)