Migrate next-sanity apps to cacheComponents - strict mode, three-layer component pattern, explicit perspective/stega/includeDrafts, prop-drilling conventions
next-sanity@cache-components)Use next-sanity@cache-components with Next.js 16+ cacheComponents: true to get Partial Prerendering with Sanity Live. This replaces the automatic draftMode() behavior with explicit perspective, stega, and includeDrafts passing.
Canary release -- not yet stable, may have breaking changes in minor/patch releases.
npm install next-sanity@cache-components --save-exact
Requires next@16+.
next.config.ts// next.config.ts
import type {NextConfig} from 'next'
import {sanity} from 'next-sanity/live/cache-life'
const nextConfig: NextConfig = {
cacheComponents: true,
cacheLife: {sanity},
}
export default nextConfig
cacheLife('sanity') needed?sanityFetch calls cacheLife internally -- 'use cache' boundaries that call sanityFetch do not need cacheLife('sanity').
The sanity preset is only needed for 'use cache' boundaries that do not call sanityFetch but should match Sanity's revalidation timing (e.g. a cached layout shell). Without it, those boundaries default to revalidating every 15 minutes, which is unnecessary since Sanity Live handles on-demand revalidation.
If the app has no such boundaries, the cacheLife: {sanity} config can be omitted entirely.
defineLivestrict: true (when using Visual Editing)Set strict: true when the app uses Visual Editing in Sanity Studio's Presentation Tool -- especially for perspective switching with Content Releases. This ensures the app correctly handles different perspectives by making perspective, stega, and includeDrafts required at every call site (type-enforced).
How to detect: if the app renders <VisualEditing /> from next-sanity/visual-editing anywhere (typically in the root layout), it uses Visual Editing and should set strict: true.
If the app does not use Visual Editing, strict can be omitted -- the defaults (perspective: 'published', stega: false, includeDrafts: false) are sufficient.
// src/sanity/lib/live.ts
import {createClient, type QueryParams} from 'next-sanity'
import {defineLive, resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'
import {cookies, draftMode} from 'next/headers'
const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
useCdn: true,
apiVersion: 'v2025-03-04',
stega: {studioUrl: '/studio'},
})
const token = process.env.SANITY_API_READ_TOKEN
if (!token) {
throw new Error('Missing SANITY_API_READ_TOKEN')
}
export const {sanityFetch, SanityLive} = defineLive({
client,
serverToken: token,
browserToken: token,
strict: true,
})
When strict: true:
sanityFetch() requires explicit perspective and stega<SanityLive> requires explicit includeDrafts propgetDynamicFetchOptions HelperExport this from the same live.ts file. It encapsulates the draftMode() and cookies() calls that cannot be used inside 'use cache' boundaries:
// src/sanity/lib/live.ts (continued)
export interface DynamicFetchOptions {
perspective: LivePerspective
stega: boolean
isDraftMode: boolean
}
export async function getDynamicFetchOptions(): Promise<DynamicFetchOptions> {
const {isEnabled: isDraftMode} = await draftMode()
if (!isDraftMode) {
return {perspective: 'published', stega: false, isDraftMode}
}
const jar = await cookies()
const perspective = await resolvePerspectiveFromCookies({cookies: jar})
return {perspective: perspective ?? 'drafts', stega: true, isDraftMode}
}
Call this at the top level only -- in layout.tsx or page.tsx. Never call it deep in the component tree (see Prop-Drilling).
sanityFetchStaticParams HelperExport a helper for generateStaticParams to avoid repeating boilerplate:
// src/sanity/lib/live.ts (continued)
export async function sanityFetchStaticParams<const QueryString extends string>({
query,
params = {},
}: {
query: QueryString
params?: QueryParams
}) {
return client.fetch(query, params, {perspective: 'published', stega: false, useCdn: true})
}
Usage:
export async function generateStaticParams() {
return sanityFetchStaticParams({query: slugsByTypeQuery, params: {type: 'page'}})
}
Resolve isDraftMode, perspective, and stega once at the top level and prop-drill them down. This is critical:
getDynamicFetchOptions() calls draftMode() -- a dynamic API. Calling it deep in the tree shrinks the static shell and may cause unexpected Suspense fallbacks.getDynamicFetchOptions() calls cookies() -- the calling component must be wrapped in <Suspense>. This complexity belongs at the top level, not in every <Navbar> or <Footer>.The three-layer pattern (Page -> Dynamic -> Cached) should only exist in layout.tsx and page.tsx. Shared components receive resolved values as props and go straight to their cached layer.
This is the core architecture for every route.
Page/Layout (Layer 1)
├── NOT draft mode → <CachedX perspective="published" stega={false} /> (no Suspense)
└── draft mode → <Suspense fallback={...}>
<DynamicX params={params} /> (Layer 2)
└── <CachedX slug={slug} perspective={p} stega={s} /> (Layer 3)
Calls draftMode() and branches:
export default async function Page({params}: {params: Promise<{slug: string}>}) {
const {isEnabled: isDraftMode} = await draftMode()
if (isDraftMode) {
return (
<Suspense
fallback={
<Template>
<LoadingSkeleton />
</Template>
}
>
<DynamicPage params={params} />
</Suspense>
)
}
const {slug} = await params
return <CachedPage slug={slug} perspective="published" stega={false} />
}
params, render cached directly -- no Suspense, maximizes static shellparams Promise to <DynamicX> without awaiting -- let it resolve inside the Suspense boundary so the static shell streams immediatelyOnly rendered in the draft mode path. Resolves all async values:
async function DynamicPage({params}: {params: Promise<{slug: string}>}) {
const {slug} = await params
const {perspective, stega} = await getDynamicFetchOptions()
return <CachedPage slug={slug} perspective={perspective} stega={stega} />
}
Has 'use cache' and only receives plain, serializable props:
async function CachedPage({
slug,
perspective,
stega,
}: {slug: string} & Pick<DynamicFetchOptions, 'perspective' | 'stega'>) {
'use cache'
const {data} = await sanityFetch({
query: PAGE_QUERY,
params: {slug},
perspective,
stega,
})
return (
<Template>
<h1>{data?.title}</h1>
</Template>
)
}
draftMode(), cookies(), or headers() inside 'use cache'notFound() can be called inside 'use cache' -- move it there and remove the old if (!data && !isDraftMode) notFound() guard. In draft mode the Suspense boundary handles the case where a document doesn't exist yet.<Suspense> outside draft mode -- unnecessary streaming boundaries shrink the static shellslug: string) to cached components, not Promisesperspective="published" and stega={false} in the non-draft path -- never omit these or make them optional. Every call to a cached component or sanityFetch must have explicit values so cache keys are consistent and stable in production. A mixture of undefined and "published" would create duplicate cache entries for the same content.perspective and stega act as cache keys -- published and draft content get separate cache entries automatically<SanityLive> -- Pass includeDraftsWith cacheComponents: true, <SanityLive> defaults includeDrafts to false and does not read draftMode() internally. You must pass it explicitly:
<SanityLive includeDrafts={isDraftMode} />
The root layout resolves draftMode() once and wraps individual dynamic components in their own Suspense boundaries -- no <DynamicLayout> wrapper needed:
// src/app/(site)/layout.tsx
import {draftMode} from 'next/headers'
import {VisualEditing} from 'next-sanity/visual-editing'
import {getDynamicFetchOptions, SanityLive} from '@/sanity/lib/live'
import {Navbar} from '@/components/Navbar'
import {Footer} from '@/components/Footer'
import {Suspense} from 'react'
export default async function SiteLayout({children}: {children: React.ReactNode}) {
const {isEnabled: isDraftMode} = await draftMode()
return <CachedLayout isDraftMode={isDraftMode}>{children}</CachedLayout>
}
function CachedLayout({children, isDraftMode}: {children: React.ReactNode; isDraftMode: boolean}) {
return (
<div className="flex min-h-screen flex-col">
{isDraftMode ? (
<Suspense fallback={<Navbar perspective="published" stega={false} />}>
<DynamicNavbar />
</Suspense>
) : (
<Navbar perspective="published" stega={false} />
)}
<main>{children}</main>
{isDraftMode ? (
<Suspense fallback={<Footer perspective="published" stega={false} />}>
<DynamicFooter />
</Suspense>
) : (
<Footer perspective="published" stega={false} />
)}
<SanityLive includeDrafts={isDraftMode} />
{isDraftMode && <VisualEditing />}
</div>
)
}
async function DynamicNavbar() {
const {perspective, stega} = await getDynamicFetchOptions()
return <Navbar perspective={perspective} stega={stega} />
}
async function DynamicFooter() {
const {perspective, stega} = await getDynamicFetchOptions()
return <Footer perspective={perspective} stega={stega} />
}
Key points:
draftMode() is called once in SiteLayout and the result is passed to <CachedLayout> -- calling draftMode() in a layout does not prevent the layout from being included in the static shell. The <CachedLayout> component ensures the shell HTML is still prerendered.<Navbar>, <Footer>) gets its own <Suspense> boundary with the published cached version as fallback -- users see cached content while draft content streams in<DynamicNavbar> and <DynamicFooter> are thin wrappers defined inline that call getDynamicFetchOptions() and pass the result as props<div>, <main>, etc.) is never wrapped in SuspenseWith prop-drilling, shared components become simple -- no draftMode(), no getDynamicFetchOptions(), no internal Suspense. The layout handles all of that. The component just receives props and renders cached:
// src/components/Navbar.tsx
import {sanityFetch, type DynamicFetchOptions} from '@/sanity/lib/live'
import {settingsQuery} from '@/sanity/lib/queries'
export async function Navbar({
perspective,
stega,
}: Pick<DynamicFetchOptions, 'perspective' | 'stega'>) {
'use cache'
const {data} = await sanityFetch({query: settingsQuery, perspective, stega})
return (
<header className="sticky top-0 z-10 flex items-center gap-4 bg-white/80 px-4 py-4 backdrop-blur">
{data?.menuItems?.map((item) => (
<a key={item._key} href={item.slug}>
{item.title}
</a>
))}
</header>
)
}
The layout wraps <Navbar> in <Suspense> when in draft mode (see Layout Example). The Navbar itself doesn't know or care about draft mode.
// src/app/(site)/[slug]/page.tsx
import {draftMode} from 'next/headers'
import {defineQuery} from 'next-sanity'
import {
getDynamicFetchOptions,
sanityFetch,
sanityFetchStaticParams,
type DynamicFetchOptions,
} from '@/sanity/lib/live'
import {Suspense} from 'react'
import type {Metadata, ResolvingMetadata} from 'next'
const PAGE_QUERY = defineQuery(
`*[_type == "page" && slug.current == $slug][0]{_id, _type, title, body}`,
)
const SLUGS_QUERY = defineQuery(`*[_type == $type && defined(slug.current)]{"slug": slug.current}`)
type Props = {params: Promise<{slug: string}>}
export async function generateStaticParams() {
return sanityFetchStaticParams({query: SLUGS_QUERY, params: {type: 'page'}})
}
// --- generateMetadata ---
export async function generateMetadata(
{params}: Props,
parent: ResolvingMetadata,
): Promise<Metadata> {
const {slug} = await params
const {perspective} = await getDynamicFetchOptions()
const data = await cachedPageMetadata({slug, perspective})
return {
title: data?.title,
}
}
async function cachedPageMetadata({
slug,
perspective,
}: {slug: string} & Pick<DynamicFetchOptions, 'perspective'>) {
'use cache'
const {data} = await sanityFetch({
query: PAGE_QUERY,
params: {slug},
perspective,
stega: false,
})
return data
}
// --- Page ---
export default async function PageRoute({params}: Props) {
const {isEnabled: isDraftMode} = await draftMode()
if (isDraftMode) {
return (
<Suspense fallback={<Template>Loading...</Template>}>
<DynamicPageRoute params={params} />
</Suspense>
)
}
const {slug} = await params
return <CachedPageRoute slug={slug} perspective="published" stega={false} />
}
async function DynamicPageRoute({params}: Props) {
const {slug} = await params
const {perspective, stega} = await getDynamicFetchOptions()
return <CachedPageRoute slug={slug} perspective={perspective} stega={stega} />
}
async function CachedPageRoute({
slug,
perspective,
stega,
}: {slug: string} & Pick<DynamicFetchOptions, 'perspective' | 'stega'>) {
'use cache'
const {data} = await sanityFetch({query: PAGE_QUERY, params: {slug}, perspective, stega})
return (
<Template>
<h1>{data?.title}</h1>
</Template>
)
}
function Template({children}: {children: React.ReactNode}) {
return <div className="space-y-6">{children}</div>
}
generateMetadata Rulesperspective via getDynamicFetchOptions() -- this supports published content in production, drafts in draft mode, and Content Releases perspective switching in Presentation Toolstega: false -- stega encoding must never appear in titles, descriptions, or OG metadatacachedMetadata function with 'use cache' -- do NOT put 'use cache' on generateMetadata itself since it needs to call getDynamicFetchOptions()generateMetadata and a cached component (where stega may be true), you must split into a separate cachedMetadata function. Never reuse a shared cached function that passes stega through.Template component that mirrors the cached component's layout. Better experience in Presentation Tool during Visual Editing. These fallbacks are only visible in draft mode (never in production), so they don't need high-quality designs or pixel-perfect skeletons -- a simple "Loading..." text inside the same layout shell is fine. CLS/web vitals are not affected since production never hits these Suspense boundaries.<CachedX perspective="published" stega={false} /> as the Suspense fallback. While technically possible, this causes problems:
next dev<Template>Loading...</Template>) or fallback={null} rather than stale contentTemplate component for the static HTML shell. Reuse in both the Suspense fallback and the cached component output.cacheComponents: falsesanityFetch no longer reads draftMode() automatically -- pass perspective and stega explicitly<SanityLive> no longer reads draftMode() automatically -- pass includeDrafts explicitlysanityFetch calls cacheTag() and cacheLife() internally -- no manual cache tag managementsanityFetchStaticParams (or client.fetch() directly) for generateStaticParams -- not sanityFetchexport const dynamic = 'force-static' -- not needed with cacheComponentsnotFound() inside the cached component -- remove old if (!data && !isDraftMode) notFound() guards. In draft mode the Suspense boundary handles documents that don't exist yetSources:
PyTorch深度学习模式与最佳实践,用于构建稳健、高效且可复现的训练流程、模型架构和数据加载。