Token导航 LogoToken导航TokenDH.com
前端设计操作浏览器github未标认证来源可访问许可证需确认审计通过

sanity-live-cache-components健全的实时缓存组件

Agent Skill

sanity-live-cache-components 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

380

周安装

16

GitHub Stars

949

下载量

133
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:sanity-live-cache-components(健全的实时缓存组件)
来源仓库:https://github.com/sanity-io/next-sanity
仓库路径:skills/sanity-live-cache-components
安装命令:
npx skills add https://github.com/sanity-io/next-sanity --skill sanity-live-cache-components
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/sanity-io/next-sanity --skill sanity-live-cache-components

简介

sanity-live-cache-components 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息。

  • 适合在 Codex、Claude、Cursor、Gemini CLI 中围绕仓库状态、代码变更或协作事项进行整理。
  • 通过 npx skills add 命令从指定 GitHub 仓库安装,需结合原始 README 确认具体用法。
  • 安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。
  • 当前分类为前端设计,暂无更多功能说明。

SKILL.md

Sanity Live + Cache Components (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.

1. Install

npm install next-sanity@cache-components --save-exact

Requires next@16+.


2. Configure 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

When is 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.


3. Configure defineLive

strict: 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 prop
  • Missing values produce type errors and runtime errors

4. getDynamicFetchOptions Helper

Export 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).


5. sanityFetchStaticParams Helper

Export 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'}})
}

6. Prop-Drilling

Resolve isDraftMode, perspective, and stega once at the top level and prop-drill them down. This is critical:

  1. getDynamicFetchOptions() calls draftMode() -- a dynamic API. Calling it deep in the tree shrinks the static shell and may cause unexpected Suspense fallbacks.
  2. In draft mode, 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.


7. The Three-Layer Component Pattern

This is the core architecture for every route.

Structure

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)

Layer 1: Page Component

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} />
}
  • Not in draft mode: await params, render cached directly -- no Suspense, maximizes static shell
  • In draft mode: pass the params Promise to <DynamicX> without awaiting -- let it resolve inside the Suspense boundary so the static shell streams immediately

Layer 2: Dynamic Component

Only 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} />
}

Layer 3: Cached Component

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>
  )
}

Rules

  • Never call 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.
  • Never wrap cached components in <Suspense> outside draft mode -- unnecessary streaming boundaries shrink the static shell
  • Always pass plain values (slug: string) to cached components, not Promises
  • Always pass explicit perspective="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

8. <SanityLive> -- Pass includeDrafts

With cacheComponents: true, <SanityLive> defaults includeDrafts to false and does not read draftMode() internally. You must pass it explicitly:

<SanityLive includeDrafts={isDraftMode} />

9. Layout Example

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.
  • Each dynamic component (<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
  • The static shell (<div>, <main>, etc.) is never wrapped in Suspense

10. Shared Component Example (Navbar)

With 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.


11. Page Example (Dynamic Route)

// 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>
}

12. generateMetadata Rules

  • Always resolve perspective via getDynamicFetchOptions() -- this supports published content in production, drafts in draft mode, and Content Releases perspective switching in Presentation Tool
  • Always hard-code stega: false -- stega encoding must never appear in titles, descriptions, or OG metadata
  • Delegate to a separate cachedMetadata function with 'use cache' -- do NOT put 'use cache' on generateMetadata itself since it needs to call getDynamicFetchOptions()
  • If a query is shared between 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.

13. Suspense Fallback Strategy

  • Preferred: meaningful loading UI using a sync 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.
  • Discouraged: using <CachedX perspective="published" stega={false} /> as the Suspense fallback. While technically possible, this causes problems:

- Triggers warnings in next dev - Shows published content briefly before draft content streams in -- confusing for editors who don't realize they're in a loading state - Can cause layout shift when the draft content replaces the published content - If no meaningful skeleton exists, use a simple placeholder (e.g. <Template>Loading...</Template>) or fallback={null} rather than stale content

  • Extract a Template component for the static HTML shell. Reuse in both the Suspense fallback and the cached component output.

14. Key Differences from cacheComponents: false

  • sanityFetch no longer reads draftMode() automatically -- pass perspective and stega explicitly
  • <SanityLive> no longer reads draftMode() automatically -- pass includeDrafts explicitly
  • sanityFetch calls cacheTag() and cacheLife() internally -- no manual cache tag management
  • Use sanityFetchStaticParams (or client.fetch() directly) for generateStaticParams -- not sanityFetch
  • Remove export const dynamic = 'force-static' -- not needed with cacheComponents
  • Move notFound() inside the cached component -- remove old if (!data &&!isDraftMode) notFound() guards. In draft mode the Suspense boundary handles documents that don't exist yet

Sources:

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

34.76%
按下载量换算46

Claude

29.72%
按下载量换算40

Cursor

17.59%
按下载量换算23

Gemini CLI

10.78%
按下载量换算14

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

操作浏览器

该 Skill 可能涉及浏览器控制能力,使用时可能读取或操作网页内容,需要在受控环境中确认权限边界。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills