Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readWeb Platform

Gzip uploads from the browser

Use CompressionStream to gzip a JSON upload in the browser and tell the receiving server how to decode the request body.

Your browser sends a day's worth of log events as JSON. The entries repeat the same keys and values, but the upload carries each repetition. If you own the receiving endpoint, you can gzip the body before you send it.

Compress the bytes before fetch

CompressionStream('gzip') takes bytes in and produces gzip bytes out. Turn the JSON into a Blob, pipe its byte stream through the compressor, then collect the output in a second blob. The second blob is the body you hand to fetch. See the CompressionStream interface.

diagram

From JSON to gzip and back

The browser sends the gzip blob, and the server uses the encoding header to recover the JSON.

Run this in a page served by your app. Give the app a same-origin POST /api/logs handler that accepts gzip and returns a successful status after it parses the JSON.

const events = Array.from({ length: 1000 }, () => ({ kind: 'saved' }))
const json = new Blob([JSON.stringify(events)], { type: 'application/json' })
const compressed = json.stream().pipeThrough(new CompressionStream('gzip'))
const body = await new Response(compressed).blob()

console.log(json.size, body.size)

const response = await fetch('/api/logs', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Encoding': 'gzip',
  },
  body,
})
if (!response.ok) throw new Error(`Upload failed (${response.status})`)

The first number is 17001 bytes. Chromium compressed these repeated events to 102 bytes. Your compressed size can vary by browser and payload. In the Network panel, the POST should carry both headers and return the handler's success status.

Decode before parsing on the server

Content-Type describes the JSON. Content-Encoding tells the receiver to undo gzip before interpreting that type. The HTTP semantics specification defines the two headers together.

Check whether your server stack already decodes compressed request bodies. If it leaves the raw bytes to your route, gunzip them there before calling the JSON parser. If middleware already decoded them, use its output directly. A handler that only accepts plain JSON can reject the request or fail while parsing the compressed bytes.

Collecting the stream with blob() buffers the compressed output before the network request starts. The original JSON blob also holds the uncompressed bytes during compression. Keep the batch small enough to hold both and use the two blob sizes to check the savings.

Try the two byte counts with your real event data before enabling the change. For a small payload, the gzip header and the compression step can cost more than the few bytes you save. Keep the plain JSON path if the endpoint does not support gzip.