When implementing caching in Next.js applications, it is crucial to be intentional about the caching behavior for each component and function. Apply caching directives consistently and consider their implications:
export async function getCachedRandomOrder() {
'use cache'
// This random value will be cached and reused for all users
return Math.random();
}
export async function getUniqueRandomPerRequest() {
// No cache directive - will be evaluated fresh for each request
return Math.random();
}
export async function getPosts(slug) {
'use cache'
// This function's result will be cached based on the slug parameter
// Different slug values = different cache entries
const data = await fetch(`/api/posts/${slug}`)
return data.json()
}
import { cacheTag, cacheLife } from 'next/cache'
export async function getPosts(slug) {
'use cache'
cacheTag('posts') // Tag this cache entry for targeted invalidation
cacheLife(60 * 60 * 1000) // Cache for 1 hour instead of default period
const data = await fetch(`/api/posts/${slug}`)
return data.json()
}
Adopting a strategic approach to cache configuration in Next.js applications can significantly improve performance while ensuring data consistency and freshness.
Enter the URL of a public GitHub repository