Point0by 1gr14
  • Menu
    • GitHub
  • Introduction
    • Overview
    • Getting Started
    • Full Overview
    • Benchmarks
    • Points
  • Points
    • Page
    • Layout
    • Component
    • Provider
    • Mountable
    • Query
    • Infinite Query
    • Mutation
    • Action
    • Root
    • Base
    • Plugin
  • Methods
    • Validation
    • Loader
    • Context
    • Middleware
    • Loading & error
    • .with
    • Mapper
    • Transformer
    • Stage Methods
  • Core
    • Navigation
    • SSR
    • RSC
    • Request
    • Response
    • Error handling
    • Env
    • Head
    • MDX
    • Assets
    • File upload
    • OpenAPI
    • Query client
    • Events
    • Infer
  • Engine
    • Engine Config
    • Engine Runtime
    • CLI
    • Dev
    • Build
    • Compiler
    • Generator
    • Project MCP
    • Docs MCP
    • Importer
    • Public dir
    • Testing
    • Deploy
    • Bun or Vite
  • Extra
    • SsrStore
    • CookieStore
    • Basic Auth
    • CORS
    • Cache-Control
    • Compress
  • Examples
    • Basic
    • Vite
    • Better Auth
    • Capacitor
    • Expo
  • 1gr14
    • Home
    • Start0
    • Support
    • Author
  • Community
    • Discord
    • Telegram
  • Open Source
    • Point0
    • Route0
    • Error0
    • Flat
  • Account
    • Sign In
    • Sign Up
Point0by 1gr14
Building open-source software for the glory
of the Lord Jesus Christ ☦️
With love for developers
of all backgrounds around the world ❤️
Terms of ServicePrivacy PolicySergei Dmitriev 2026 😎

Compress

  • Category: Extra

On this page

  1. Streaming, not buffering
  2. What gets compressed
  3. Encoding negotiation
  4. Per-response control: tune and tuneCompress
  5. From config: the tune option
  6. From a handler: tuneCompress()
  7. The guardrails
  8. Reference
  9. compress(options)
  10. Behavior at a glance
  11. Exports

@point0/compress compresses responses at the origin. compress(options) returns a Point0 middleware: mount it on your root and every compressible response — HTML, JSON, JS/CSS chunks, SVG — ships brotli- or gzip-encoded, per the client's Accept-Encoding.

import { Point0 } from '@point0/core'
import { compress } from '@point0/compress'

export const root = Point0.lets
  .root()
  .middleware(compress()) // ← text responses ship brotli/gzip encoded
  // ...
  .root()

It earns its keep wherever responses leave the server uncompressed — an app served straight from its host with no CDN edge in front, where the ~100 KB SSR HTML would otherwise go over the wire as-is. And since that SSR HTML is private, no-store (see cache-control), no CDN would cache and compress it for you anyway — the origin is the only place it can happen.

Streaming, not buffering

The body is piped through the compressor (node:zlib) with a flush after every incoming chunk:

  • a streamed SSR document keeps its progressive delivery — the shell reaches the browser as soon as React emits it, instead of sitting in the compressor's window until enough bytes accumulate;
  • no second copy of the body is ever held in memory, so there is no response size limit.

Because the compressed size isn't known ahead of the stream, Content-Length is dropped and the response goes out chunked. Vary: Accept-Encoding is appended so caches key the variants correctly, and a strong ETag is weakened to W/… (the compressed bytes are an equivalent, not identical, representation).

What gets compressed

Text-like content types: text/*, image/svg+xml, and application/* that is JSON, XML, JavaScript, or wasm (including suffixed types like manifest+json). Images, fonts, video, and archives arrive already compressed and are left alone.

The middleware skips by itself:

  • internal server-to-server fetches (request.from.server) — they are consumed programmatically, not sent over the wire;
  • already-encoded responses (Content-Encoding present);
  • HEAD, bodyless, and 204/205/304 responses;
  • text/event-stream — SSE frames must not be re-chunked;
  • known-small bodies: a response with a Content-Length under minBytes (default 1024) isn't worth the encoding overhead. Streams of unknown length always compress.

Encoding negotiation

Accept-Encoding is parsed with honest q-values: br;q=0 is an explicit refusal, * covers encodings the client didn't list. Among the encodings the client accepts, the option's preference order decides — the default ['br', 'gzip'] prefers brotli (denser) and falls back to gzip (universal):

compress({ encodings: ['gzip'] }) // gzip only — e.g. to save origin CPU
compress({ brotliQuality: 4 }) //    default 5 — the origin sweet spot
compress({ gzipLevel: 9 }) //        default: zlib's own (6)

Per-response control: tune and tuneCompress

Compression has one per-response decision, reachable from two sides. Both yield the same value — false / true / a settings object / undefined:

  • false — skip this response;
  • true — compress it even when its content type isn't in the built-in set (you know the payload is compressible);
  • an object { encodings?, brotliQuality?, gzipLevel?, minBytes? } — compress it with these per-response settings (each falls back to the global option); this also forces past the content-type check;
  • undefined — the default content-type decision.

From config: the tune option

tune decides per response from the config. It receives the same detailed result object the middleware got back from next() (response, request, variant, scope, error):

compress({
  tune: (result) => {
    if (result.variant.type === 'endpoint') return false // never compress API responses
    if (result.variant.type === 'page') return { brotliQuality: 11 } // max density for the SSR document
    return undefined
  },
})

From a handler: tuneCompress()

tuneCompress(value) sets the same decision imperatively for the current request, and overrides the config tune (hand-set beats policy). It reads the current request through point0's getRequest() itself, so you pass only the value:

import { tuneCompress } from '@point0/compress'

.action(async () => {
  tuneCompress(false) // a long-lived LLM stream — nothing may sit between us and the client
  return new Response(llmStream, { headers: { 'content-type': 'text/plain' } })
})

tuneCompress(false) is the common opt-out (a stream that must not be buffered or re-chunked); tuneCompress(true) forces a payload whose content type the sniff doesn't recognize; tuneCompress({ brotliQuality: 11 }) forces + tunes; tuneCompress(undefined) clears a prior call. It is scoped to the one request whose handler calls it — kept on that request's per-instance state, under an internal key (so it never shows up on your typed request.state). That scope is deliberate: a query running during a page's SSR is its own sub-request, so its tuneCompress(false) affects only that query's response, never the page document. A no-op off any request context.

The guardrails

Neither tune nor tuneCompress can un-skip the cases the middleware always leaves raw: internal server-to-server fetches, already-encoded responses, and bodyless / HEAD / 204/304 responses. They control the compression policy, not these correctness invariants.

Reference

compress(options)

Every option is optional; compress() with no argument is the correct default for a typical app.

OptionTypeDefaultWhat
encodings('br' | 'gzip')[]['br', 'gzip']preference order; first one the client accepts wins
minBytesnumber1024skip bodies with a known Content-Length under this
brotliQualitynumber (0..11)5brotli quality — near-top density at a fraction of the top levels' CPU
gzipLevelnumber (0..9)zlib defaultgzip level
tune(result) => boolean | settings | undefined—per-response: false skip, true force, settings object to force + tune, undefined default

Behavior at a glance

AspectBehavior
Sidecut from the client bundle — the .middleware(...) argument and its imports are removed, so it never ships to the browser
Deliverystreaming with a per-chunk flush — progressive SSR stays progressive, no buffering
Content-Lengthdropped on compressed responses (size unknown ahead of the stream)
VaryAccept-Encoding appended for every compressible response, even when negotiation ends with no encoding
ETaga strong validator is weakened to W/…
Opt-outtuneCompress(false) from any handler

Exports

ExportWhat
compress(options)build the middleware
tuneCompress(value)set the per-request decision from a handler (false | true | settings object | undefined); overrides the config tune
PreviousCache-ControlNextBasic Example

Enjoying Point0?

Give it a star on GitHub, ask questions, and follow what's new

Star Point0

A star on GitHub helps more developers find Point0
Star on GitHub

Start0

Point0 integrated into the Start0 SaaS boilerplate — the best way to use it
Explore Start0

YouTube

Tutorials, walkthroughs, and updates
YouTube

Discord

Questions and chat in English
Join Discord

Telegram

News channel and chat in Russian
ChannelChat

Twitter

Posts and updates
Twitter