Skip to content

Commit

Permalink
fix: make htmlFallback more permissive (#15059)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 21, 2023
1 parent 81fde80 commit 6fcceeb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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)
})

0 comments on commit 6fcceeb

Please sign in to comment.