microcms-js-sdk
microcms-js-sdk copied to clipboard
Next 15 でキャッシュを有効化できない
環境
- next: 15.3.1
- microcms-js-sdk 3.2.0
背景
関連: #53
Next 15 でキャッシュの挙動に破壊的変更が入り、 fetch 結果がデフォルトでキャッシュされなくなりました。
以前のバージョンのように fetch の結果をキャッシュするには、2025/5/6現在、以下のいずれかの対応が必要になります:
-
'use cache'ディレクティブを使用する (canary バージョンのみ) -
fetchにcache: 'force-cache'option を渡す
参考: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching#reusing-data-across-multiple-functions
問題
下記のように SDK の customRequestInit オプションを用いて fetch の cache オプションに 'force-cache' を渡しました。
// ブログ一覧を取得
export const getList = async (queries?: MicroCMSQueries) => {
const listData = await client
.getList<Blog>({
endpoint: 'blog',
queries,
customRequestInit: {
cache: 'force-cache',
},
})
.catch(notFound);
return listData;
};
しかし、 next dev でローカル開発サーバーを起動し Next の cache のログを見ると、キャッシュがスキップされていました。
│ GET https://<domain>.microcms.io/api/v1/tags?limit=10 200 in 169ms (cache skip)
│ │ Cache skipped reason: (cache-control: no-cache (hard refresh))
│ GET https://<domain>.microcms.io/api/v1/blog/btezx8p6a82 200 in 165ms (cache skip)
│ │ Cache skipped reason: (cache-control: no-cache (hard refresh))
│ GET https://<domain>.microcms.io/api/v1/blog/btezx8p6a82 200 in 147ms (cache skip)
│ │ Cache skipped reason: (cache-control: no-cache (hard refresh))
参考: https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration#troubleshooting
質問
Next 15 でキャッシュを効かせる方法はありますか。
勘違いしてました! cache は headers ではありませんでした。
-> customRequestInit に cache: 'force-cache' を指定してもキャッシュされなかったため、改訂して再オープンしました。