response package provides focused helpers for common HTTP responses with correct headers and caching semantics.
Installation
Features
- Web Standards Compliant - Built on the standard
ResponseAPI - Runtime Agnostic - Works in Node.js, Bun, Deno, Cloudflare Workers, and browsers
- File Responses - Full HTTP semantics including ETags, Last-Modified, conditional requests, and Range support
- HTML Responses - Automatic DOCTYPE prepending and proper Content-Type headers
- Redirect Responses - Simple redirect creation with customizable status codes
- Compress Responses - Streaming compression based on Accept-Encoding header
Imports
This package provides no default export. Import specific helpers:File Responses
Create responses for serving files with full HTTP semantics.createFileResponse
The file to serve (native
File or LazyFile from @remix-run/lazy-file).The incoming request object (used for conditional requests and range support).
Configuration options for the response.
FileResponseOptions
Cache-Control header value.Default:
undefined (no Cache-Control header)ETag generation strategy:
'weak': Generates weak ETags based on file size and mtime (default)'strong': Generates strong ETags by hashing file contentfalse: Disables ETag generation
'weak'Hash algorithm or custom digest function for strong ETags.
- String: Web Crypto API algorithm name (
'SHA-256','SHA-384','SHA-512','SHA-1') - Function: Custom digest computation
etag: 'strong'. Default: 'SHA-256'Whether to include
Last-Modified headers.Default: trueWhether to support HTTP Range requests for partial content.When enabled, includes
Accept-Ranges header and handles Range requests with 206 Partial Content responses.Defaults to enabling ranges only for non-compressible MIME types (as defined by isCompressibleMimeType() from @remix-run/mime).Range requests and compression are mutually exclusive. This is why ranges are only enabled by default for non-compressible types.
Features
ThecreateFileResponse helper automatically handles:
- Content-Type and Content-Length headers
- ETag generation (weak or strong)
- Last-Modified headers
- Cache-Control headers
- Conditional requests (
If-None-Match,If-Modified-Since,If-Match,If-Unmodified-Since) - Range requests for partial content (206 Partial Content)
- HEAD request support
Examples
Basic File Response
Strong ETags
For assets requiring strong validation:Custom Digest Function
For large files or custom hashing:Immutable Assets
For hashed assets that never change:FileLike Interface
The minimal interface for file-like objects:File objects and LazyFile from @remix-run/lazy-file implement this interface.
HTML Responses
Create HTML responses with proper Content-Type and DOCTYPE handling.createHtmlResponse
The HTML content to send. Accepts multiple types:
- String
- SafeHtml from
@remix-run/html-template - Blob/File
- ArrayBuffer
- ReadableStream
Standard Response initialization options (status, headers, etc.).
A Response with
Content-Type: text/html; charset=UTF-8 and automatic DOCTYPE prepending if not present.Examples
Basic HTML Response
With SafeHtml
With Custom Status
Complete Page
Redirect Responses
Create redirect responses with improved ergonomics overResponse.redirect.
createRedirectResponse
The redirect destination. Can be relative or absolute.
Status code (number) or ResponseInit object with additional options.Default:
302Unlike
Response.redirect, this helper accepts relative URLs. While not technically spec-compliant, relative redirects are widely supported and commonly used.Examples
Basic Redirect
Custom Status Code
With Additional Headers
Common Redirect Patterns
Compress Responses
Compress responses based on the client’s Accept-Encoding header.compressResponse
The response to compress.
The incoming request (used to read Accept-Encoding header).
Compression configuration options.
CompressResponseOptions
Minimum size in bytes to compress (only enforced if Content-Length is present).Default:
1024Which encodings the server supports for negotiation.Default:
['br', 'gzip', 'deflate']Node.js zlib options for gzip/deflate compression.For SSE responses (
text/event-stream), flush: Z_SYNC_FLUSH is automatically applied unless you explicitly set a flush value.Node.js zlib options referenceNode.js zlib options for Brotli compression.For SSE responses (
text/event-stream), flush: BROTLI_OPERATION_FLUSH is automatically applied unless you explicitly set a flush value.Node.js Brotli options referenceAutomatic Skip Conditions
Compression is automatically skipped for:- Responses with no
Accept-Encodingheader - Responses already compressed (existing
Content-Encoding) - Responses with
Cache-Control: no-transform - Responses with
Content-Lengthbelow threshold - Responses with range support (
Accept-Ranges: bytes) - 206 Partial Content responses
- HEAD requests (only headers are modified)
Examples
Basic Compression
Custom Threshold
Custom Encodings
Custom Compression Options
SSE (Server-Sent Events)
Range Requests and Compression
This is whycreateFileResponse enables ranges only for non-compressible MIME types by default:
Type Definitions
Related Packages
- headers - Type-safe HTTP header manipulation
- html-template - Safe HTML templating
- lazy-file - Lazy file implementation for streaming
- mime - MIME type utilities