Even if you pass explicit cache configuration options (such as cache: 'force-cache' or next: { revalidate: 60 }) to your fetch call, using cookies() in your route will still opt the entire route into dynamic rendering. This means that the fetch will not be cached by Next.js's Data Cache, and the cache options will be ignored. This is confirmed in the official documentation and GitHub issues:
In Next, using cookies() on a page opts it into dynamic rendering, which means each request will hit the server. This is explained here.
You can still opt into fetch caching by passing explicit cache configuration options to your fetch. More on fetch caching can be found here.
— However, the presence of cookies() will still force dynamic rendering and prevent caching, regardless of fetch options.
GitHub Issue: cookies break caching/memoization
Example Code
import { cookies } from "next/headers";
export default async function Page() {
const cookieStore = cookies();
const res = await fetch("<https://api.example.com/data",> {
cache: "force-cache",
next: { revalidate: 60 },
});
const data = await res.json();
return <pre>{JSON.stringify(data, null, 2)}</pre>;
}
Result:
The fetch will be executed on every request, and the cache options will be ignored because cookies() is used in the route. There is no way to enable server-side fetch caching in this scenario, according to the current Next.js documentation and issue discussions.