#Data caching when fetch request uses cookies

4 messages · Page 1 of 1 (latest)

thick meadow
#

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() {
 const res = await fetch(url, {
    headers: { cookie: cookies().toString() }
  });
 ...
}

Learn how to fetch, cache, and revalidate data in your Next.js application.

sonic igloo
#

@thick meadow - Are we speaking about OAuth and refresh and access tokens?

thick meadow
#

Yes

sonic igloo
#

An access token should be sent via Authorization header. No cookie. You only need the cookie for the refresh token and the refresh process and for the refresh process (the endpoint) you don't need caching.