Allow Client Components access to request headers during SSR #60640
Replies: 8 comments 16 replies
-
Why do you need to fetch this data in the SSR stage? Could you not fetch this data in a parent server component and pass it down? A specific use case example would help this discussion I think. |
Beta Was this translation helpful? Give feedback.
-
@shuding's tweet may be interesting here:
|
Beta Was this translation helpful? Give feedback.
-
Are there no more developments with this? |
Beta Was this translation helpful? Give feedback.
-
i use AsyncLocalStorage in middleware to store headers, then use it in client component, it seems ok. |
Beta Was this translation helpful? Give feedback.
-
Is there any way to read |
Beta Was this translation helpful? Give feedback.
-
Any news on this one? Found this thread on X and wonder if this still on the roadmap. |
Beta Was this translation helpful? Give feedback.
-
bump |
Beta Was this translation helpful? Give feedback.
-
I came across something in a codebase that seems to work, though it is undocumented: const useHeaders = () =>
typeof window === "undefined"
? global?.__incrementalCache.requestHeaders
: null I have no idea how dependable this is, though. I imagine it should certainly only work when called in the subtree of a dynamic route. And since it's undocumented, it could be removed without warning. Where I found it in use was to sniff the user agent to improve the isomorphism of a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
headers()
functionBackground
Using the pages router you could get access to the incoming request headers in getInitialProps (NextPageContext), allowing you to fetch data and forward the headers properly.
Proposal
Could we get some primitive for this, maybe a
useSSRContext
that's only filled during the SSR prepass?Note
This is already possible today, by calling
headers()
and passing it as props to the client component, although this means the headers are included in the HTML markup which isn't ideal as it exposes http only cookies to be read by JavaScript.Beta Was this translation helpful? Give feedback.
All reactions