Skip to content

Commit

Permalink
feat: add brotli and gzip support for streaming response
Browse files Browse the repository at this point in the history
  • Loading branch information
fastner committed Aug 3, 2024
1 parent 99962a4 commit f033640
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { PassThrough } from "node:stream"
import zlib from "node:zlib"

import type { AppLoadContext, EntryContext } from "@remix-run/node"
import { createReadableStreamFromReadable } from "@remix-run/node"
Expand Down Expand Up @@ -120,14 +121,24 @@ async function handleBrowserRequest(
`public, max-age=${DEFAULT_CACHE_TIME_SECS}, s-maxage=${DEFAULT_CACHE_TIME_SECS}`
)

if (request.headers.get("Accept-Encoding")?.includes("br")) {
responseHeaders.set("Content-Encoding", "br")
const brotli = zlib.createBrotliCompress()
pipe(brotli).pipe(body)
} else if (request.headers.get("Accept-Encoding")?.includes("gzip")) {
responseHeaders.set("Content-Encoding", "gzip")
const gzip = zlib.createGzip()
pipe(gzip).pipe(body)
} else {
pipe(body)
}

resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode
})
)

pipe(body)
},
onShellError(error: unknown) {
reject(error)
Expand Down

0 comments on commit f033640

Please sign in to comment.