useCookies() for reading cookies without de-opting partial static rendering #58367
colinclerk
started this conversation in
Ideas
Replies: 0 comments
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.
-
Goals
Non-Goals
Background
There is currently no optimal way to read cookies from client components during SSR. Developers have two options:
cookies()
from a parent server component and pass the value as a propcookies()
from the root layout, set a context, and read cookies from the context. This is the technique used bynext-client-cookies
https://github.com/moshest/next-client-cookies/tree/mainThe first technique offers reasonable cache behavior, but must be repeated any time a cookie is necessary for a client component.
The second technique does not require duplication, but needlessly de-opts the page from static rendering. It also represents a potential security issue, since HttpOnly cookies will be passed into the document body, potentially enabling javascript access to a value that is meant to only be accessed from the server.
Both techniques cause cookie data to needlessly be duplicated over the wire (cookies are already in a header, and passing them in a prop over the server/client boundary causes them to be duplicated in the document body)
Proposal
useCookies()
should be implemented with the same cache behavior asuseSearchParams()
. So:During the SSR run,
useCookies()
should read from the Cookie headerOn the client,
useCookies()
should read fromdocument.cookie
Cookie values should not be included in the document body.
Beta Was this translation helpful? Give feedback.
All reactions