TL;DR
Next.js 14 以前の App Router では fetch() が force-cache をfetch していると{ cache: 'no-store' } のno-cache との
深夜に気づいた「何もしていないのに壊れている」状態
NetaPair の200 OK は
Vercel の
データ
// app/matches/page.tsx
async function getRecentMatches() {
const res = await fetch(`${process.env.API_BASE_URL}/api/matches/recent`);
if (!res.ok) throw new Error('fetch failed');
return res.json();
}
fetch に
Next.js 14 の fetch は「デフォルトでキャッシュ済み」として動く
App Router の Server Component 内では、fetch を Next.js がcache: 'force-cache' として
// 以下の 2 つは Next.js 14 では同じ挙動
const res1 = await fetch('https://api.example.com/data');
const res2 = await fetch('https://api.example.com/data', { cache: 'force-cache' });
一度revalidateTag や revalidatePath をrevalidate の
この
参照: Next.js 14 Caching ドキュメント
no-store と no-cache: 似ているが別物
最初のcache: 'no-cache' にすればno-cache を
原因は HTTP の
| オプション | キャッシュへ | 使用前の | 実際の |
|---|---|---|---|
force-cache | する | しない | 永久 |
no-cache | する | 毎回する | 保存は |
no-store | しない | しない | キャッシュを |
no-cache はno-store にしなければ
no-store は
修正: 動的データには no-store を明示する
// app/matches/page.tsx
async function getRecentMatches() {
const res = await fetch(`${process.env.API_BASE_URL}/api/matches/recent`, {
cache: 'no-store',
});
if (!res.ok) throw new Error('fetch failed');
return res.json();
}
これだけで、
データの性質ごとの使い分け
| データの | 推奨 | 具体例 |
|---|---|---|
| 毎 | cache: 'no-store' | マッチング |
| 数分〜数時間で | next: { revalidate: N } | ブログ |
| ビルド | cache: 'force-cache' | 静的 |
ISR スタイルでnext.revalidate が
// 5 分ごとに再取得(それまでは古いデータを返してよい場合)
const res = await fetch('https://api.example.com/data', {
next: { revalidate: 300 },
});
ページ全体を動的にする場合
コンポーネントno-store を
// app/matches/page.tsx の先頭に追加
export const dynamic = 'force-dynamic';
これで
Next.js 15 での変更点
Next.js 15 でこのfetch の
つまり v15 ではforce-cache に
v14 → v15 のcache オプションをforce-cache / no-store / revalidate を
まとめ
- Next.js 14 以前の App Router では
fetchはデフォルトで force-cache(ビルド 時 キャッシュ) - 動的
データには 必ず { cache: 'no-store' }を明示する no-cacheとno-storeは別物。 「毎回 フレッシュに 取得する」 なら no-store- ページ
全体を 動的に するなら export const dynamic = 'force-dynamic'が手軽 - Next.js 15 では
デフォルトが 変わったが、 明示的な 指定で バージョン 非依存な 設計に する 方が 堅牢