perf(server): gzip compress session messages response

Large sessions can have 20MB+ JSON payloads which take 20-30s to
transfer over mobile networks. Gzip compression reduces this to
~2-3MB, improving mobile load times by ~5x.
This commit is contained in:
Britt Lewis
2026-01-30 18:00:23 -05:00
parent e834a2e6c9
commit 20e56a4971

View File

@@ -579,6 +579,17 @@ export const SessionRoutes = lazy(() =>
sessionID: c.req.valid("param").sessionID,
limit: query.limit,
})
const acceptEncoding = c.req.header("accept-encoding") ?? ""
if (acceptEncoding.includes("gzip")) {
const json = JSON.stringify(messages)
const compressed = Bun.gzipSync(json)
return new Response(compressed, {
headers: {
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
})
}
return c.json(messages)
},
)