how to get user-agent header in layout.ts file
Answered
hareshFF posted this in #help-forum
hareshFFOP
hi, can i access headers in layout file, i need the user-agent header. i know i can access it middleware. but in my case i need it in layout, so i can forward the user-agent to a different api server that is called to initialise react context data for global state.
Answered by gin
if your layout is a rsc u can do that https://nextjs.org/docs/app/api-reference/functions/headers#headers
4 Replies
if your layout is a rsc u can do that https://nextjs.org/docs/app/api-reference/functions/headers#headers
Answer
import { headers } from 'next/headers'
export default function Page() {
const headersList = headers()
const referer = headersList.get('referer')
return <div>Referer: {referer}</div>
}keep in mind this doesnt work on client components