Server headers
Edit page
Learn how to set custom HTTP headers for all server route responses in Expo Router.
Server headers are available in SDK 54 and later, and requiresexpo-serverto serve your exported application.
Server headers in Expo Router allow you to set custom HTTP headers for security, caching, cookies, and custom metadata on route responses. Headers only apply to HTML and API route responses, and are not applicable to static assets such as images, fonts, or JavaScript bundles.
Setup
1
Configure headers in the expo-router plugin in your app config:
{ "expo": { "plugins": [ [ "expo-router", { "headers": { "X-Frame-Options": "DENY" } } ] ] } }
2
Start the development server or export for production:
-Â npx expo start# Or export for production-Â npx expo export -p webHeaders are automatically applied to all HTML and API route responses.
Configuration
Headers are configured as an object where keys are header names and values are either strings or arrays of strings.
{ "expo": { "plugins": [ [ "expo-router", { "headers": { "X-Frame-Options": "DENY", "X-Content-Type-Options": "nosniff", "Set-Cookie": ["session=abc123; HttpOnly", "preference=dark; Path=/"] } } ] ] } }
Examples
Security headers
Add common security headers to protect your application:
{ "expo": { "plugins": [ [ "expo-router", { "headers": { "X-Frame-Options": "DENY", "X-Content-Type-Options": "nosniff", "Referrer-Policy": "strict-origin-when-cross-origin", "X-XSS-Protection": "1; mode=block" } } ] ] } }
Cache-Control headers
Set caching policies for your responses:
{ "expo": { "plugins": [ [ "expo-router", { "headers": { "Cache-Control": "public, max-age=3600, s-maxage=86400" } } ] ] } }
Custom headers
Add custom headers with metadata about your app:
{ "expo": { "plugins": [ [ "expo-router", { "headers": { "X-App-Version": "1.0.0", "X-Environment": "production" } } ] ] } }
How it works
Output modes
Server headers work with both output modes configured in your app config:
static: Headers are applied when serving pre-rendered HTML files withexpo-serverserver: Headers are applied to dynamically rendered responses
Header precedence
Headers defined in the expo-router plugin are applied globally but do not override headers set by API routes. If an API route returns a response with a header that is also defined in the plugin configuration, the route-specific header takes precedence.
For example, if you configure Cache-Control: public, max-age=3600 globally, but an API route that returns real-time data sets Cache-Control: no-store, the API route's header takes precedence.
Known limitations
- Redirects: Headers do not apply to redirect responses
- Static assets: Headers are only applied to HTML and API route responses, not to static assets like images, fonts, or JavaScript bundles
Related
Learn how to create server endpoints with Expo Router.
Learn how to create middleware that runs for every request to the server.