The screenshot API for developers -
Try ScreenshotOne
Skip to content

Response Compression Plugin

Compresses response bodies to reduce bandwidth usage and improve performance for clients that support compression.

Server

Use ResponseCompressionHandlerPlugin to compress response bodies. The plugin selects an encoding based on Accept-Encoding header:

handler
ts
import { ResponseCompressionHandlerPlugin } from '@orpc/server/plugins'

const handler = new RPCHandler(router, {
  plugins: [
    new ResponseCompressionHandlerPlugin({
      /**
       * The compression schemes to use for response compression.
       * Schemes are prioritized by their order in this array and
       * only applied if the client supports them.
       * Supported values: 'gzip' | 'deflate' | 'deflate-raw'
       *
       * @default ['gzip', 'deflate']
       */
      encodings: ['gzip', 'deflate'],

      /**
       * The minimum response size in bytes required to trigger compression.
       * Responses smaller than this threshold will not be compressed to avoid overhead.
       * If the response size cannot be determined, compression will still be applied.
       *
       * @default 1024 (1KB)
       */
      threshold: 1024,
    }),
  ],
})

INFO

The handler can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or a custom one.

Client

Use ResponseCompressionLinkPlugin to advertise supported encodings via Accept-Encoding header and automatically decompress response bodies based on the Content-Encoding header:

ts
import { ResponseCompressionLinkPlugin } from '@orpc/client/plugins'

const link = new RPCLink({
  plugins: [
    new ResponseCompressionLinkPlugin({
      /**
       * Compression schemes to advertise via Accept-Encoding, in preference order.
       * Supported values: 'gzip' | 'deflate' | 'deflate-raw'
       *
       * @default ['gzip', 'deflate']
       */
      encodings: ['gzip', 'deflate'],
    }),
  ],
})

INFO

The link can be any supported oRPC link, such as RPCLink, OpenAPILink, or a custom one.

WARNING

When using the fetch adapter, this plugin is usually unnecessary because most fetch implementations automatically set the Accept-Encoding header and decompress response bodies.

Learn More

For implementation details, see the ResponseCompressionLinkPlugin source code or the ResponseCompressionHandlerPlugin source code.

Released under the MIT License.