What is the `useRequestHeaders` composable used for?
Answer
useRequestHeaders() is an SSR-specific Nuxt composable that retrieves incoming HTTP request headers within the server rendering context. It's primarily used for cookie forwarding during SSR — when your Nuxt server makes internal API calls (e.g., useFetch('/api/profile')), the user's authentication cookies must be forwarded so the API can identify the user. Without this, server-side fetches would be unauthenticated. Example: const headers = useRequestHeaders(['cookie']); const { data } = await useFetch('/api/user', { headers }). It returns an empty object on the client side (where request headers don't exist), making it safe to use in universal code. This composable only works during SSR; in client-only contexts it has no effect.
Previous
How do you implement WebSocket connections in a Nuxt.js server?
Next
How do you implement streaming SSR in Nuxt.js?
More Nuxt.js Questions
View all →- Advanced How does Nuxt.js universal rendering (SSR + CSR hydration) work?
- Advanced How do you optimize performance in a Nuxt.js application?
- Advanced What is the island components feature in Nuxt.js?
- Advanced How do you implement custom Nuxt.js modules?
- Advanced How does Nuxt.js handle edge rendering?