Cookies and cache
Answered
Masai Lion posted this in #help-forum
Masai LionOP
If the server component makes a call for cookies, how does the nextjs caching work? Does it just not cache anymore, meaning if there's an expensive operation not related to cookies in the same component, it will be called every time?
Answered by Eric Burel
(today, to achieve that, you would need to mix static rendering + client components with requests from the browser, we can't mix static and dynamic server-side rendering)
26 Replies
Cookies will switch your page to dynamic, because cookies are tied to the request => the render depends on each HTTP request and thus can't be cached
the page could still be cached in the user browser though, but not shared between users
however, it seems that Partial Rendering could allow scoping the dynamicness to specific components
you could have say 90% of the page static
and 10% dynamic components that will load and then appear
(today, to achieve that, you would need to mix static rendering + client components with requests from the browser, we can't mix static and dynamic server-side rendering)
Answer
it has been announced at the Next conf so we still expect more information on that
Masai LionOP
so, if I have a wrapper server component that does expensive operations, pass that down into another server component that does the cookie stuff, will that be fine?
oh, nvm I see, we can mix them, thanks then
@Eric Burel (today, to achieve that, you would need to mix static rendering + client components with requests from the browser, we can't mix static and dynamic server-side rendering)
Masai LionOP
actually, would it be possible to put the expensive operation in a layout file, then have the cookies in the page server component?
no actually
let me share you the ticket about that
You should log when the layout renders to see how often it renders
because I am not sure if what I mentions in the issue is exactly your issue
you may want to give it a shot, if you don't need generateStaticParams in the layout that would work but then I am not 100% how often the layout will be rendered
basically if the page is dynamic, the layout is too
you may simply avoid rerenders during client-side navigation
but the layout won't be static basically
Masai LionOP
oof, ok, thanks for the help, I'll do some testing then to see what renders, thanks!
Highlander
@Masai Lion Were you able to figure this out? I think we’re both trying to solve something similar.
I have a root layout component that requires data coming from an API, but that data doesn’t require auth. My leaf server component requires auth and uses the
What happens for me is that the leaf component runs first and since the
Based on the documentation, I thought my
I have a root layout component that requires data coming from an API, but that data doesn’t require auth. My leaf server component requires auth and uses the
cookies function. What happens for me is that the leaf component runs first and since the
cookies function is called, the root layout component will fetch the data again instead of using the cache. Based on the documentation, I thought my
await for data in the root layout would be a blocking request for any nodes beneath it, but unfortunately the leaf RSC always runs first and prevents usage of cached data in any other component.@Highlander <@325030038649176064> Were you able to figure this out? I _think_ we’re both trying to solve something similar.
I have a root layout component that requires data coming from an API, but that data doesn’t require auth. My leaf server component requires auth and uses the `cookies` function.
What happens for me is that the leaf component runs first and since the `cookies` function is called, the root layout component will fetch the data again instead of using the cache.
Based on the documentation, I thought my `await` for data in the root layout would be a blocking request for any nodes beneath it, but unfortunately the leaf RSC _always_ runs first and prevents usage of cached data in any other component.
Masai LionOP
if the leaf isnt part of the layout, it shouldn't cause any other pages to lose cache. I stuck to changing my server components to using unstable_cache for my solution, that worked great
Highlander
Looks like
I’m hoping that partial pre-rendering makes this possible without manually using the
unstable_cache should work for my use case at the moment as well. Thanks a bunch!I’m hoping that partial pre-rendering makes this possible without manually using the
unstable_cache though 🤞Highlander
Ah, I found another solution. I moved the data fetching to a separate RSC and used that in the page, rather than fetching directly in the page.
It wasn’t immediately obvious, but it seems like the rendering order is something like
It wasn’t immediately obvious, but it seems like the rendering order is something like
Page > Root Layout > Nested Layouts > Component in Page. I was thinking that the nested layouts would waterfall before the page, so my mental model was a bit off.