# Next.js: Route used `cookies()` inside a function cached with `unstable_cache(...)`. Accessing Dynamic data sources inside a cache scope is not supported

**Code:** `Used cookies/headers inside a function cached with unstable_cache` · **Category:** Next.js · **Seen on:** Next.js, React

A cached function reads request-specific data (cookies, headers), which would leak between users.

## Meaning

Read `cookies()`/`headers()` outside the cached function and pass the needed values as arguments (they become part of the cache key). Cached output must depend only on its arguments. The same rule applies to `"use cache"` functions.

## Common causes

- `cookies()`/`headers()` called in `unstable_cache` callback
- Auth/user data read inside a cached helper
- Search params accessed within the cache scope
- Shared data function used both cached and uncached
- Hidden dynamic access in a dependency

## Quick fix

1. `const uid = (await cookies()).get("uid")?.value; const getData = unstable_cache(fn, [uid])` pass uid as an argument
2. Move user-specific logic outside the cache
3. Cache only public data

## Detailed fix by platform

### Next.js

1. `const getPosts = unstable_cache(async (uid: string) => db.posts(uid), ["posts"], { revalidate: 60 });`

## How to diagnose

1. **Source** — cookies/headers inside cache?
2. **Args** — Passed in?
3. **Data** — User-specific?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/nextjs/unstable-cache-dynamic-data-inside)
