response provides focused helpers for common HTTP responses with correct headers and caching semantics.
Features
- Web Standards Compliant: Built on the standard
ResponseAPI, works in any JavaScript runtime - 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
Installation
Usage
This package provides no default export. Instead, import the specific helper you need:File Responses
ThecreateFileResponse helper creates a response for serving files with full HTTP semantics. It works with both native File objects and LazyFile from @remix-run/lazy-file:
Features
- 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
Options
Strong ETags and Content Hashing
For assets that require strong validation (e.g., to supportIf-Match preconditions), configure strong ETag generation:
'SHA-256' algorithm. You can customize this:
Conditional Requests
createFileResponse automatically handles conditional requests:
Range Requests
Support partial content delivery for large files:HTML Responses
ThecreateHtmlResponse helper creates HTML responses with proper Content-Type and DOCTYPE handling:
<!DOCTYPE html> if not already present. It works with strings, SafeHtml from @remix-run/html-template, Blobs/Files, ArrayBuffers, and ReadableStreams.
With SafeHtml
Complete Pages
Redirect Responses
ThecreateRedirectResponse helper creates redirect responses. The main improvements over the native Response.redirect API are:
- Accepts a relative
locationinstead of a full URL - Accepts a
ResponseInitobject as the second argument, allowing you to set additional headers and status code
Common Redirect Patterns
Compress Responses
ThecompressResponse helper compresses a Response based on the client’s Accept-Encoding header:
- Responses with no
Accept-Encodingheader - Responses that are already compressed (existing
Content-Encoding) - Responses with
Cache-Control: no-transform - Responses with
Content-Lengthbelow threshold (default: 1024 bytes) - Responses with range support (
Accept-Ranges: bytes) - 206 Partial Content responses
- HEAD requests (only headers are modified)
Options
Usage with Middleware
API Reference
createFileResponse(file, request, options?)
Create a response for serving files with full HTTP semantics.
Parameters:
file: File- The file to serverequest: Request- The incoming requestoptions?: FileResponseOptions- Configuration options
Promise<Response>
createHtmlResponse(html, init?)
Create an HTML response with proper Content-Type.
Parameters:
html: string | SafeHtml | Blob | ArrayBuffer | ReadableStream- The HTML contentinit?: ResponseInit- Additional response options
Response
createRedirectResponse(location, init?)
Create a redirect response.
Parameters:
location: string- The redirect location (can be relative)init?: number | ResponseInit- Status code or response options (default: 302)
Response
compressResponse(response, request, options?)
Compress a response based on Accept-Encoding.
Parameters:
response: Response- The response to compressrequest: Request- The incoming requestoptions?: CompressOptions- Compression configuration
Promise<Response>
Related Packages
headers- Type-safe HTTP header manipulationhtml-template- Safe HTML templating with automatic escapingfile-storage- File storage utilitiesfetch-router- Build HTTP routers using the web fetch API