From 6fcceebe34863c4fcde809885976b12cf5398fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Tue, 21 Nov 2023 22:27:41 +0900 Subject: [PATCH] fix: make htmlFallback more permissive (#15059) --- .../src/node/server/middlewares/htmlFallback.ts | 3 +-- playground/html/__tests__/html.spec.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) 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) +})