Skip to content

Commit

Permalink
fix: allow path ending with .html to fallback to index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored and patak-dev committed Sep 29, 2023
1 parent ec7ee22 commit dae6d0a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/vite/src/node/server/middlewares/htmlFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ export function htmlFallbackMiddleware(
return spaFallback ? `/index.html` : request.url
},
},
// don't rewrite paths ending with .html
{
from: /\.html$/,
to({ request }: any) {
return request.url
to({ parsedUrl, request }: any) {
// .html files are not handled by serveStaticMiddleware
// so we need to check if the file exists
const pathname = decodeURIComponent(parsedUrl.pathname)
if (fs.existsSync(path.join(root, pathname))) {
return request.url
}
return '/index.html'
},
},
],
Expand Down
4 changes: 4 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ test('should get a 404 when using incorrect case', async () => {
)
})

test('should fallback to index.html when accessing non-existant html file', async () => {
expect((await fetchPath('doesnt-exist.html')).status).toBe(200)
})

describe('injected scripts', () => {
test('@vite/client', async () => {
const hasClient = await page.$(
Expand Down

0 comments on commit dae6d0a

Please sign in to comment.