OpenAINext.jsOct 24, 2024, 2:00 PM

Our Journey with Caching

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

openaienmodel: gpt-5-mini-2025-08-07

Our Journey with Caching

Key Points

  • Two-mode model: 'use cache' (static) vs <Suspense> (dynamic)
  • Tagging and TTL control with cacheTag and cacheLife
  • Experimental opt-in via canary + experimental.dynamicIO

Summary

Next.js introduces an experimental, simpler caching model built on two concepts: using <Suspense> to opt into dynamic (uncached, streaming) server data, or marking a segment as static/cached with the "use cache" directive. Async functions can also be made cached by adding "use cache" inside them. New cache primitives—cacheTag and cacheLife—let you tag and control lifetimes, and cache keys are derived automatically from arguments and closures. This undoes the previous default caching of fetch(); you’ll get build/runtime errors if you use async data without choosing a mode. The feature is experimental and opt-in (canary + experimental.dynamicIO).

Key Points

  • Two-mode model:
    • <Suspense> => dynamic data fetching, streaming, no implicit caching.
    • "use cache" (per page/layout/function) => whole segment is cached and can be statically rendered.
  • If a segment uses async data without declaring a mode, the runtime/build will throw an error to force an explicit choice.
  • You can add "use cache" inside any async function to create a cached function; cache key auto-includes args and closures.
  • cacheTag(...) lets you tag entries and later call revalidateTag(...) from a Server Action to invalidate by tag.
  • cacheLife("minutes" | "hours" | "days" | "weeks" | "max" | etc.) controls cache lifetime/stale and expire behavior without exact seconds math.
  • Partial caching is supported: mark layouts/pages independently and mix static and dynamic segments.
  • Experimental: enable via canary (npx create-next-app@canary) and next.config.ts experimental.dynamicIO; recommended only for greenfield or testing now.

Practical advice for engineers:

  • For iterative or highly dynamic pages, wrap data components with <Suspense>.
  • For pages you want statically rendered, add "use cache" at the top of the page/layout or inside specific async helper functions.
  • Use cacheTag + revalidateTag for targeted invalidation and cacheLife for approximate TTLs.

Full Translation

Translations

A translation section that keeps the flow of the original article.

openaijamodel: gpt-5-mini-2025-08-07

キャッシュに関する私たちの歩み

キャッシュに関する私たちの歩み

Next.js App Routerにおけるキャッシュの取り組みについて学びます。この記事では私たちが経験した課題、試したアプローチ、そして得られた教訓を共有します。

  • 対象読者: Next.js App Routerを利用している開発者
  • 取り上げる内容: 実装の選択肢、パフォーマンス上の考慮点、キャッシュ戦略の比較
  • 目的: より安定で高速なアプリケーションを構築するための実践的な知見の提供

本文では具体的なケーススタディやベストプラクティスを順を追って説明します。