Next.js Discord

Discord Forum

Fallback to cached-components when cookies are not present

Unanswered
Egyptian Mau posted this in #help-forum
Open in Discord
Egyptian MauOP
For sure, I'm totally aware of why caching is a bad idea when relying on cookie-data in most cases.
But is there any chance to check for cookies and somehow fallback to cached-components when they are not present or in a specific state? (not logged in).

6 Replies

Egyptian MauOP
Context: A public page which has to be seo-optimized as much as possible.
This page loads dynamic but cachable data but when a logged-in user accesses it, the data is being enhanced with some more (partially user-specific) details.
Pacific sand lance
i guess it's possible
something like

async function SomeComponent(...) {
  const cookieStore = await cookies();
  const state = cookieStore.get("...")?.value === "..."; // or whatever you want
  if (state) return <SomeOtherComponent state={state} />;
  return <SomeCachedComponent />;
}
if there is no cookie, it will resolve almost immediately
also i believe, <template> tag(s) injected by nextjs won't hurt your seo as they are present in initial html (content is present) and almost instantly patched