headers
A server-side function that defines HTTP headers to be sent with the document response. Only runs on document requests, not data requests.Signature
Arguments passed to the headers function
Can return:
- A Headers object
- A HeadersInit object (plain object, array of tuples)
Basic Example
Using Loader Headers
Merging Parent Headers
Conditional Headers Based on Action
Error Response Headers
Security Headers
Cache Control Strategies
Content Security Policy
Dynamic Headers
Setting Cookies
Best Practices
Set headers in loaders, merge in headers()
Set headers in loaders, merge in headers()
Keep header logic close to the data:
Leaf route headers win
Leaf route headers win
React Router uses headers from the deepest matching route:
Don't set Content-Type or Content-Length
Don't set Content-Type or Content-Length
These are automatically managed by React Router:
Consider the full header hierarchy
Consider the full header hierarchy
Remember that headers bubble up through parent routes: