Capítulo 203 de 456
The cacheLife config object in next.config.js defines custom, named cache profiles (stale/revalidate/expire) that the cacheLife() function consumes inside use cache scopes.
cacheLife config object: maps a profile name (e.g. blog) to { stale, revalidate, expire }, set alongside cacheComponents: true.default, seconds, minutes, hours, days, weeks, or max by declaring a profile with the same name.revalidate).import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
cacheLife: {
blog: {
stale: 3600, // 1 hour
revalidate: 900, // 15 minutes
expire: 86400, // 1 day
},
},
}
export default nextConfig
blog no next.config.ts.import { cacheLife } from 'next/cache'
export async function getCachedData() {
'use cache'
cacheLife('blog')
const res = await fetch('https://api.example.com/data')
const data = await res.json()
return data
}
blog dentro de uma função com use cache.| Property | Value | Description | Requirement |
|---|---|---|---|
stale | number | duração que o cliente cacheia sem checar o servidor | Optional |
revalidate | number | frequência de refresh do cache no servidor | Optional |
expire | number | duração máxima de valor stale antes de virar dinâmico | Optional, deve ser maior que revalidate |
expire menor ou igual a revalidate: viola o requisito documentado (expire deve ser maior).cacheComponents: true habilitado para funcionar.cacheLife('nome') dentro de 'use cache'.default, seconds, etc.) sobrescreve o comportamento padrão dele globalmente.cacheLife() é chamado.