Next.js Discord

Discord Forum

How to update headers in root layout

Unanswered
Red-breasted Merganser posted this in #help-forum
Open in Discord
Red-breasted MerganserOP
I need to get headers generated in my middleware inside some components rendered in the root layout.tsx in app dir. As this layout.tsx is shared inside my whole app, it doesn't re-render when I the route change.

Is there any way to get the updated headers in these components?

5 Replies

Have you tried using headers() function?

https://nextjs.org/docs/app/api-reference/functions/headers
Red-breasted MerganserOP
Hi @Clown, yes I'm using it to get the headers ! But my issue is that the layout doesn't re-render on route change, so the headers are not updated inside
Hmm... i guess that makes sense.

Maybe a "template" would work?
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#templates
However from what ik, even if Layout isnt supposed to be re-rendered a lot, but dynamic functions should still update? I only know about usePathname() working tho and have never tried header in layout before.
Red-breasted MerganserOP
I just tried with a template and it doesn't seems to re-render ; also usePathname only works in a client component and header only works in a server component :/