diff --git a/packages/vite/src/node/server/middlewares/htmlFallback.ts b/packages/vite/src/node/server/middlewares/htmlFallback.ts index d5f053fd34211c..5af9a34b2df2dd 100644 --- a/packages/vite/src/node/server/middlewares/htmlFallback.ts +++ b/packages/vite/src/node/server/middlewares/htmlFallback.ts @@ -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('*/*') ) diff --git a/playground/html/__tests__/html.spec.ts b/playground/html/__tests__/html.spec.ts index 01f0eca2e7e445..a026ce71cf2a3d 100644 --- a/playground/html/__tests__/html.spec.ts +++ b/playground/html/__tests__/html.spec.ts @@ -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) +})