この例では 3600秒(1時間)サーバーサイドでキャッシュが有効になります。
page.tsx
/**
* Route Segment Config
*/
export const revalidate = 3600
export default async function Page() {
fetch('https://...')
unstable_cache を使ってキャッシュ関数を合成することで、キャッシュに対応できるようになります。
公式: https://nextjs.org/docs/app/api-reference/functions/unstable_cache
page.tsx
/**
* Route Segment Config
*/
export const revalidate = 3600
/**
* cached Function
*/
const getUserCached = unstable_cache(
(id:string) => { return getUser(id) } // データ取得関数をここに
[keyParts],
)
export default async function Page() {
const user = getUserCached('xxxxxxxx')
Route Segment Configは以下のデフォルト値を持ちます。
export const dynamic = 'auto'
export const dynamicParams = true
export const revalidate = false
export const fetchCache = 'auto'
export const runtime = 'nodejs'
export const preferredRegion = 'auto'
export const maxDuration = 5
Option | Type | 説明 |
---|---|---|
dynamic | 'auto' | 'force-dynamic' | 'error' | 'force-static' | 'auto' |
dynamicParams | boolean | true |
revalidate | false | 0 | number | false |
fetchCache | 'auto' | 'default-cache' | 'only-cache' | 'force-cache' | 'force-no-store' | 'default-no-store' | 'only-no-store' | 'auto' |
runtime | 'nodejs' | 'edge' | 通常はnodejs。Cloudflareなどにデプロイする場合は edge |
preferredRegion | 'auto' | 'global' | 'home' | string | string[] | 'auto' |
maxDuration | number | サーバーの最大実行時間。これを超えるとエラーとなる。デプロイプラットフォームによって自動設定されます。 |