Skip to content

Commit 0edb243

Browse files
authored
perf(serve-static): performance optimization for precompressed feature (#3414)
* perf(serve-static): use "Set" for checking precompressed * refactor: define COMPRESSIBLE_CONTENT_TYPE_REGEX in utils * refactor: set "Content-Type" header only if the content is defined * perf(serve-static): find compressed file only if the mime type is compressible
1 parent 5f78e4e commit 0edb243

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed

src/middleware/compress/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
*/
55

66
import type { MiddlewareHandler } from '../../types'
7+
import { COMPRESSIBLE_CONTENT_TYPE_REGEX } from '../../utils/compress'
78

89
const ENCODING_TYPES = ['gzip', 'deflate'] as const
910
const cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/i
10-
const compressibleContentTypeRegExp =
11-
/^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i
1211

1312
interface CompressionOptions {
1413
encoding?: (typeof ENCODING_TYPES)[number]
@@ -68,7 +67,7 @@ export const compress = (options?: CompressionOptions): MiddlewareHandler => {
6867

6968
const shouldCompress = (res: Response) => {
7069
const type = res.headers.get('Content-Type')
71-
return type && compressibleContentTypeRegExp.test(type)
70+
return type && COMPRESSIBLE_CONTENT_TYPE_REGEX.test(type)
7271
}
7372

7473
const shouldTransform = (res: Response) => {

src/middleware/serve-static/index.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ describe('Serve Static Middleware', () => {
127127
expect(await res.text()).toBe('Hello in static/hello.html.br')
128128
})
129129

130+
it('Should return a pre-compressed brotli response - /static/hello.unknown', async () => {
131+
const app = new Hono().use(
132+
'*',
133+
baseServeStatic({
134+
getContent,
135+
precompressed: true,
136+
})
137+
)
138+
139+
const res = await app.request('/static/hello.unknown', {
140+
headers: { 'Accept-Encoding': 'wompwomp, gzip, br, deflate, zstd' },
141+
})
142+
143+
expect(res.status).toBe(200)
144+
expect(res.headers.get('Content-Encoding')).toBe('br')
145+
expect(res.headers.get('Vary')).toBe('Accept-Encoding')
146+
expect(await res.text()).toBe('Hello in static/hello.unknown.br')
147+
})
148+
130149
it('Should not return a pre-compressed response - /static/not-found.txt', async () => {
131150
const app = new Hono().use(
132151
'*',
@@ -167,6 +186,26 @@ describe('Serve Static Middleware', () => {
167186
expect(await res.text()).toBe('Hello in static/hello.html')
168187
})
169188

189+
it('Should not find pre-compressed files - /static/hello.jpg', async () => {
190+
const app = new Hono().use(
191+
'*',
192+
baseServeStatic({
193+
getContent,
194+
precompressed: true,
195+
})
196+
)
197+
198+
const res = await app.request('/static/hello.jpg', {
199+
headers: { 'Accept-Encoding': 'gzip, br, deflate, zstd' },
200+
})
201+
202+
expect(res.status).toBe(200)
203+
expect(res.headers.get('Content-Encoding')).toBeNull()
204+
expect(res.headers.get('Vary')).toBeNull()
205+
expect(res.headers.get('Content-Type')).toMatch(/^image\/jpeg/)
206+
expect(await res.text()).toBe('Hello in static/hello.jpg')
207+
})
208+
170209
it('Should return response object content as-is', async () => {
171210
const body = new ReadableStream()
172211
const response = new Response(body)

src/middleware/serve-static/index.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import type { Context, Data } from '../../context'
77
import type { Env, MiddlewareHandler } from '../../types'
8+
import { COMPRESSIBLE_CONTENT_TYPE_REGEX } from '../../utils/compress'
89
import { getFilePath, getFilePathWithoutDefaultDocument } from '../../utils/filepath'
910
import { getMimeType } from '../../utils/mime'
1011

@@ -23,6 +24,7 @@ const ENCODINGS = {
2324
zstd: '.zst',
2425
gzip: '.gz',
2526
} as const
27+
const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS) as (keyof typeof ENCODINGS)[]
2628

2729
const DEFAULT_DOCUMENT = 'index.html'
2830
const defaultPathResolve = (path: string) => path
@@ -96,29 +98,27 @@ export const serveStatic = <E extends Env = Env>(
9698
return c.newResponse(content.body, content)
9799
}
98100

99-
const mimeType = options.mimes
100-
? getMimeType(path, options.mimes) ?? getMimeType(path)
101-
: getMimeType(path)
101+
if (content) {
102+
const mimeType = options.mimes
103+
? getMimeType(path, options.mimes) ?? getMimeType(path)
104+
: getMimeType(path)
102105

103-
if (mimeType) {
104-
c.header('Content-Type', mimeType)
105-
}
106+
if (mimeType) {
107+
c.header('Content-Type', mimeType)
108+
}
106109

107-
if (content) {
108-
if (options.precompressed) {
109-
const acceptEncodings =
110+
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) {
111+
const acceptEncodingSet = new Set(
110112
c.req
111113
.header('Accept-Encoding')
112114
?.split(',')
113115
.map((encoding) => encoding.trim())
114-
.filter((encoding): encoding is keyof typeof ENCODINGS =>
115-
Object.hasOwn(ENCODINGS, encoding)
116-
)
117-
.sort(
118-
(a, b) => Object.keys(ENCODINGS).indexOf(a) - Object.keys(ENCODINGS).indexOf(b)
119-
) ?? []
120-
121-
for (const encoding of acceptEncodings) {
116+
)
117+
118+
for (const encoding of ENCODINGS_ORDERED_KEYS) {
119+
if (!acceptEncodingSet.has(encoding)) {
120+
continue
121+
}
122122
const compressedContent = (await getContent(path + ENCODINGS[encoding], c)) as Data | null
123123

124124
if (compressedContent) {

src/utils/compress.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @module
3+
* Constants for compression.
4+
*/
5+
6+
/**
7+
* Match for compressible content type.
8+
*/
9+
export const COMPRESSIBLE_CONTENT_TYPE_REGEX =
10+
/^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i

0 commit comments

Comments
 (0)