🐡

【Next.js15】Error: During prerendering, fetch() rejects when...

に公開

サーバーコンポーネントでfetchをすると下記エラーが

Error: [Error: During prerendering, fetch() rejects when the prerender is complete. Typically these errors are handled by React but if you move fetch() to a different context by using setTimeout, after, or similar functions you may observe this error and you should handle it in that context.] {
  expression: 'fetch()',
  digest: 'HANGING_PROMISE_REJECTION'
}

AI解説

Next.js 15ではデフォルトでキャッシュが無効化されたため、静的生成時に使用するfetchには明示的にキャッシュ設定を指定する必要があります

結論

fetch関数にキャッシュを付ければよい

"use cache"

Discussion