Cacheproductpricingthatneedstobefetchedatrequesttimebutcanbesharedacrossallusers.Use[`cacheLife`](/docs/app/api-reference/functions/cacheLife#custom-cache-profiles) to set the cache lifetime of the price.
- Content is personalized per-user (depends on cookies, headers)
- You need [runtime prefetching](/docs/app/guides/prefetching) of user-specific content
- Content should never be shared between users
## How it works
The `'usecache:remote'` directive enables runtime caching of shared data in dynamic contexts by storing results in server-side cache handlers rather than prerendering at build time.
### Dynamic context detection
When Next.js encounters certain APIs like [`connection()`](/docs/app/api-reference/functions/connection), [`cookies()`](/docs/app/api-reference/functions/cookies), or [`headers()`](/docs/app/api-reference/functions/headers), the context becomes "dynamic". In a dynamic context:
1. **Regular `use cache` stops working** - it won'tcacheanything
1. Cached data is shared across all users and requests
2. Cache entries persist beyond a single session
3. Cache invalidation works via [`cacheTag`](/docs/app/api-reference/functions/cacheTag) and [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag)
4. Cache expiration is controlled by [`cacheLife`](/docs/app/api-reference/functions/cacheLife) configuration
### Dynamic context example
```tsx
async function UserDashboard() {
// Calling connection() makes the context dynamic
await connection()
// Without any caching directive, this runs on every request
const stats = await getStats()
// With 'usecache:remote', this is cached in the remote handler
const analytics = await getAnalytics()
return (
<div>
<Stats data={stats} />
<Analytics data={analytics} />
</div>
)
}
async function getAnalytics() {
'usecache:remote'
cacheLife({ expire: 300 }) // 5 minutes
// This expensive operation is cached and shared across all requests
return fetchAnalyticsData()
}
```
## Request APIs and remote caches
While `'usecache:remote'` technically allows access to request-specific data by calling API'slike[`cookies()`](/docs/app/api-reference/functions/cookies)and[`headers()`](/docs/app/api-reference/functions/headers)beforecallingafunctionthatiswrappedby`'use cache: remote'`,it's generally not recommended to use them together:
| API | Allowed in `use cache` | Allowed in `'usecache:remote'` | Recommended |
| [`cookies()`](/docs/app/api-reference/functions/cookies) | No | No | Use [`'usecache:private'`](/docs/app/api-reference/directives/use-cache-private) instead |
| [`headers()`](/docs/app/api-reference/functions/headers) | No | No | Use [`'usecache:private'`](/docs/app/api-reference/directives/use-cache-private) instead |
| [`connection()`](/docs/app/api-reference/functions/connection) | No | No | No - these cannot ever be cached |
| [`searchParams`](/docs/app/api-reference/file-conventions/page#searchparams-optional) | No | No | Use [`'usecache:private'`](/docs/app/api-reference/directives/use-cache-private) instead |
> **Important:** If you need to cache based on cookies, headers, or search params, use [`'usecache:private'`](/docs/app/api-reference/directives/use-cache-private) instead. Remote caches are shared across all users, so caching user-specific data in them can lead to incorrect results being served to different users.
## Nesting rules
Remote caches have specific nesting rules:
- Remote caches **can** be nested inside other remote caches (`'usecache:remote'`)
- Remote caches **can** be nested inside regular caches (`'usecache'`)
- Remote caches **cannot** be nested inside private caches (`'usecache:private'`)
- Private caches **cannot** be nested inside remote caches
```tsx
// VALID: Remote inside remote
async function outerRemote() {
'usecache:remote'
const result = await innerRemote()
return result
}
async function innerRemote() {
'usecache:remote'
return getData()
}
// VALID: Remote inside regular cache
async function outerCache() {
'usecache'
// If this is in a dynamic context, the inner remote cache will work
const result = await innerRemote()
return result
}
async function innerRemote() {
'usecache:remote'
return getData()
}
// INVALID: Remote inside private
async function outerPrivate() {
'usecache:private'
const result = await innerRemote() // Error!
return result
}
async function innerRemote() {
'usecache:remote'
return getData()
}
// INVALID: Private inside remote
async function outerRemote() {
'usecache:remote'
const result = await innerPrivate() // Error!
return result
}
async function innerPrivate() {
'usecache:private'
return getData()
}
```
## Examples
The following examples demonstrate common patterns for using `'usecache:remote'`. For details about `cacheLife` parameters (`stale`, `revalidate`, `expire`), see the [`cacheLife` API reference](/docs/app/api-reference/functions/cacheLife).
### Per-request database queries
Cache expensive database queries that are accessed in dynamic contexts, reducing load on your database:
```tsx filename="app/dashboard/page.tsx"
import { connection } from 'next/server'
import { cacheLife, cacheTag } from 'next/cache'
export default async function DashboardPage() {
// Make context dynamic
await connection()
const stats = await getGlobalStats()
return <StatsDisplay stats={stats} />
}
async function getGlobalStats() {
'usecache:remote'
cacheTag('global-stats')
cacheLife({ expire: 60 }) // 1 minute
// This expensive database query is cached and shared across all users,
// reducing load on your database
const stats = await db.analytics.aggregate({
total_users: 'count',
active_sessions: 'count',
revenue: 'sum',
})
return stats
}
```
### API responses in streaming contexts
Cache API responses that are fetched during streaming or after dynamic operations:
> - Remote caches are stored in server-side cache handlers and shared across all users
> - Remote caches work in dynamic contexts where regular [`use cache`](/docs/app/api-reference/directives/use-cache) would fail
> - Use [`cacheTag()`](/docs/app/api-reference/functions/cacheTag) and [`revalidateTag()`](/docs/app/api-reference/functions/revalidateTag) to invalidate remote caches on-demand
> - Use [`cacheLife()`](/docs/app/api-reference/functions/cacheLife) to configure cache expiration
> - For user-specific data, use [`'usecache:private'`](/docs/app/api-reference/directives/use-cache-private) instead of `'usecache:remote'`
> - Remote caches reduce origin load by storing computed or fetched data server-side