Next.js Discord

Discord Forum

Data caching when fetch request uses cookies

Answered
American Sable posted this in #help-forum
Open in Discord
American SableOP
From what I understand (from https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching), if your fetch request uses cookies, data will not be cached. So if I need to pass, say, a session cookie or an access token from a server component for every API request, data will never be cached?

Example:
import { cookies } from 'next/headers';

export default async function ServerComponent() {
// always cache miss
 const res = await fetch(url, {
    headers: { cookie: cookies().toString() }
  });
 ...
}
Answered by DirtyCajunRice | AppDir
you can cache fetch. just set revalidate. the page will be dynamic.
View full answer

3 Replies