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
7 changes: 6 additions & 1 deletion src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ export const serveStatic = <E extends Env = any>(
const mimeType = getMimeType(path)
c.header('Content-Type', mimeType || 'application/octet-stream')

if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) {
if (
options.precompressed &&
(!mimeType ||
mimeType === 'application/octet-stream' ||
COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))
) {
const acceptEncodingSet = new Set(
c.req
.header('Accept-Encoding')
Expand Down
1 change: 1 addition & 0 deletions test/assets/static-with-precompressed/hello.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Not Compressed
1 change: 1 addition & 0 deletions test/assets/static-with-precompressed/hello.bin.br
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello br Compressed
12 changes: 12 additions & 0 deletions test/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ describe('Serve Static Middleware', () => {
expect(res.text).toBe('Hello Not Compressed')
})

it('Should return a pre-compressed response for an octet-stream file - /static-with-precompressed/hello.bin', async () => {
const res = await request(server)
.get('/static-with-precompressed/hello.bin')
.set('Accept-Encoding', 'gzip, br, zstd')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toBe('application/octet-stream')
expect(res.headers['content-length']).toBe('19')
expect(res.headers['content-encoding']).toBe('br')
expect(res.headers['vary']).toBe('Accept-Encoding')
expect(res.body.toString()).toBe('Hello br Compressed')
})

describe('Absolute path', () => {
const rootPaths = [
path.join(__dirname, 'assets'),
Expand Down