From 27a57c3f6772e8f08580076a2ead95cf1f2a7677 Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Tue, 17 Sep 2024 10:07:36 +0900 Subject: [PATCH] fix(serve-static): use application/octet-stream if the mime type is not detected (#3415) --- src/middleware/serve-static/index.test.ts | 1 + src/middleware/serve-static/index.ts | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/middleware/serve-static/index.test.ts b/src/middleware/serve-static/index.test.ts index 585008ca7..3f0c493f6 100644 --- a/src/middleware/serve-static/index.test.ts +++ b/src/middleware/serve-static/index.test.ts @@ -143,6 +143,7 @@ describe('Serve Static Middleware', () => { expect(res.status).toBe(200) expect(res.headers.get('Content-Encoding')).toBe('br') expect(res.headers.get('Vary')).toBe('Accept-Encoding') + expect(res.headers.get('Content-Type')).toBe('application/octet-stream') expect(await res.text()).toBe('Hello in static/hello.unknown.br') }) diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 6c45d61ac..d9cdfbc06 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -99,13 +99,8 @@ export const serveStatic = ( } if (content) { - const mimeType = options.mimes - ? getMimeType(path, options.mimes) ?? getMimeType(path) - : getMimeType(path) - - if (mimeType) { - c.header('Content-Type', mimeType) - } + const mimeType = (options.mimes && getMimeType(path, options.mimes)) || getMimeType(path) + c.header('Content-Type', mimeType || 'application/octet-stream') if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) { const acceptEncodingSet = new Set(