Replies: 2 comments
-
I guess we need to fetch it in a server component and pass it to a client component as a prop. Makes react development more complicated. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Importantly, this is not generally ~~secure~~. Authorization cookies should generally™ be |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
It should be possible to securely access cookies from the request context while server-side rendering (SSR) client components. Currently it's only possible to access request cookies from React server components (RSC).
By securely, I mean that the cookie value should not be serialized in the resulting HTML document produced by the request. This is important to ensure that HTTP-only cookies cannot be read by JavaScript code (which helps prevent XSS attacks and is often required for compliance reasons in many organizations).
Non-Goals
No response
Background
There is currently no way to access cookies (or other headers) when doing SSR for
"use client"
components. This matters when trying to do (e.g.,) isomorphic fetch using suspense. I'm running into this problem when trying to use Apollo with GraphQL for "classic" SSR (not RSC) where I want the data to be fetched on the server for performance but to be also refetched on the client in response to user interaction. The same issue applies to usingfetch
in client components.To illustrate, imagine something like:
Related links
A handful of people have requested this. There are related discussions on the next.js repository (though those are more specific that this one I believe).
Proposal
It's beyond my expertise to suggest a proposal for this because NextJS + RSC + SSR is very complicated. Naively, I'd like to just be able to use
cookies()
orheaders()
inside client components but throw an error on the browser. Alternatively, maybe a separatessrCookies()
function if we still want usingcookies()
to raise an error at build time when used in client components.Beta Was this translation helpful? Give feedback.
All reactions