Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions packages/vite/src/node/server/middlewares/htmlFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ export function htmlFallbackMiddleware(
if (
// Only accept GET or HEAD
(req.method !== 'GET' && req.method !== 'HEAD') ||
// Ignore JSON requests
req.headers.accept?.includes('application/json') ||
// Require Accept: text/html or */*
!(
req.headers.accept === undefined || // equivalent to `Accept: */*`
req.headers.accept === '' || // equivalent to `Accept: */*`
req.headers.accept.includes('text/html') ||
req.headers.accept.includes('*/*')
)
Expand Down
16 changes: 16 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,19 @@ test('html serve behavior', async () => {
expect(bothSlashIndexHtml.status).toBe(200)
expect(await bothSlashIndexHtml.text()).toContain('both/index.html')
})

test('html fallback works non browser accept header', async () => {
expect((await fetch(viteTestUrl, { headers: { Accept: '' } })).status).toBe(
200,
)
// defaults to "Accept: */*"
expect((await fetch(viteTestUrl)).status).toBe(200)
// wait-on uses axios and axios sends this accept header
expect(
(
await fetch(viteTestUrl, {
headers: { Accept: 'application/json, text/plain, */*' },
})
).status,
).toBe(200)
})