fix(serve-static): serve precompressed files for application/octet-stream#366
fix(serve-static): serve precompressed files for application/octet-stream#366yusukebe wants to merge 1 commit into
Conversation
|
Hi @yusukebe, By the way, is the reason you didn't embed it directly into I understand the desire to distinguish this from the However, I think there’s also the view that, in the case of I feel it might also be a good idea to allow passing diff --git i/src/serve-static.ts w/src/serve-static.ts
index 9daa4c8..bea3535 100644
--- i/src/serve-static.ts
+++ w/src/serve-static.ts
@@ -12,7 +12,7 @@ export type ServeStaticOptions<E extends Env = Env> = {
root?: string
path?: string
index?: string // default is 'index.html'
- precompressed?: boolean
+ precompressed?: boolean | ((options: { path: string; mimeType?: string }) => boolean)
rewriteRequestPath?: (path: string, c: Context<E>) => string
onFound?: (path: string, c: Context<E>) => void | Promise<void>
onNotFound?: (path: string, c: Context<E>) => void | Promise<void>
@@ -65,6 +65,12 @@ export const serveStatic = <E extends Env = any>(
console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`)
}
+ const precompressedFilter: (options: { path: string; mimeType?: string }) => boolean =
+ typeof options.precompressed === 'function'
+ ? options.precompressed
+ : ({ mimeType }) =>
+ options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))
+
return async (c, next) => {
// Do nothing if Response is already set
if (c.finalized) {
@@ -108,7 +114,7 @@ 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 (precompressedFilter({ path, mimeType })) {
const acceptEncodingSet = new Set(
c.req
.header('Accept-Encoding')I thought about something like that, but I think it’s perfectly fine to target |
Fixes #360
serveStatic({ precompressed: true })didn't serve precompressed variants forapplication/octet-streamfiles like.bin, even when a.br/.zst/.gzexisted. Now it does.