Summary
Create responses that containheaders and status without forcing serialization into an actual Response object.
The data() function allows you to attach metadata like HTTP headers and status codes to your loader/action data while keeping type safety and avoiding premature serialization.
Signature
Parameters
D
required
The data to be included in the response. Can be any serializable value including objects, arrays, primitives, etc.
number | ResponseInit
Either a status code number or a
ResponseInit object containing:status- HTTP status code (e.g., 200, 201, 404)statusText- Status text messageheaders- Response headers object or Headers instance
Returns
DataWithResponseInit<D>
A
DataWithResponseInit instance containing the data and response initialization options.Examples
Basic usage
Return data without any special headers or status:With status code
With custom headers
With status and headers
Common Use Cases
Setting cache headers
Control how browsers and CDNs cache your responses:Error responses with status codes
Setting cookies
Content negotiation
Rate limiting headers
Type Safety
Thedata() function preserves TypeScript types:
Comparison with Response
You can also return a nativeResponse object:
data() when you want type safety and automatic serialization. Use Response when you need fine-grained control over the response format.
Related Functions
Notes
- You don’t need to use
data()if you’re just returning plain data without custom headers/status - The data is not serialized until it’s sent to the client, allowing for efficient server-side processing
- Headers are merged with any headers set by middleware or the framework
- Status codes should follow HTTP standards for proper client handling