Apply the Three-Layer Pattern for Sanity Live
mainTo correctly use Sanity Live with Next.js Cache Components, follow a three-layer architecture to separate dynamic API calls (like params or cookies) from cached data fetching.
The Pattern Structure
- Layer 1: Page/Layout (Draft Mode Branch): The top-level component. It handles
draftMode()logic. Do not use'use cache'here. - Layer 2: Dynamic Component: A component that awaits dynamic APIs (e.g.,
params,searchParams, orgetDynamicFetchOptions). This layer should be wrapped in<Suspense>when in draft mode. - Layer 3: Cached Component: The component that actually performs the data fetch using
sanityFetch. This is the only layer that should contain the'use cache'directive.
Visual Representation
Page/Layout (Layer 1: draftMode branch)
├── NOT draft mode → <CachedX perspective="published" stega={false} /> (no Suspense)
└── draft mode → <Suspense fallback={...}>
<DynamicX params={params} /> (Layer 2: awaits dynamic APIs)
└── <CachedX perspective={p} stega={s} /> (Layer 3: 'use cache')Critical Rule: Adding 'use cache' to the top-level Page or Layout function is a failure mode. Dynamic APIs are forbidden inside 'use cache' boundaries. Layer 3's use of 'use cache' is sufficient to allow the route to prerender into a static shell.