Skip to content

Cannot access cookies() or headers() in "use cache"

Source URL: https://nextjs.org/docs/messages/next-request-in-use-cache

Cannot access cookies() or headers() in "use cache"

Section titled “Cannot access cookies() or headers() in "use cache"”

A function is trying to read from the current incoming request inside the scope of a function annotated with "use cache". This is not supported because it would make the cache invalidated by every request which is probably not what you intended.

Instead of calling this inside the "use cache" function, move it outside the function and pass the value in as an argument. The specific value will now be part of the cache key through its arguments.

Before:

import { cookies } from 'next/headers'
async function getExampleData() {
"use cache"
const isLoggedIn = (await cookies()).has('token')
...
}
export default async function Page() {
const data = await getExampleData()
return ...
}

After:

import { cookies } from 'next/headers'
async function getExampleData(isLoggedIn) {
"use cache"
...
}
export default async function Page() {
const isLoggedIn = (await cookies()).has('token')
const data = await getExampleData(isLoggedIn)
return ...
}