# sanity-live-cache-components

Connects next-sanity to Next.js Cache Components: sanityFetch behind one shared 'use cache' boundary, with <SanityLive> keeping cached pages fresh.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 527 installs · ★ 957
- Author: sanity-io
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/sanity-io/next-sanity/tree/HEAD/skills/sanity-live-cache-components/SKILL.md
- Updated: 2026-10-23
- HTML: https://affpapa.org/en/nevibecode/skills/sanity-live-cache-components

## What it does

This skill sets up `next-sanity` v13+ inside a Next.js 16 app that has `cacheComponents: true` turned on. Queries go through `sanityFetch` and a single shared `'use cache'` boundary called `cachedSanity`; a `<SanityLive>` component in the root layout revalidates cached content over an EventSource connection to Sanity Content Lake. Draft mode, Visual Editing and Presentation Tool keep working alongside it.

The skill walks a fixed order. First `defineLive` and the `live.ts` helpers, then a pass over every `sanityFetch` call site so `perspective` and `stega` come from the right source, then wiring `<SanityLive>`/`<VisualEditing>` and draft mode, and finally lifting the remaining cache opt-outs on Sanity routes. Components are split into a Page / Dynamic / Cached layout with `perspective` and `stega` passed down explicitly as props.

You need Next.js 16.3+ actually installed (check `package.json` or `npm ls next`, not the registry), an `AGENTS.md` file, and three environment variables: `NEXT_PUBLIC_SANITY_PROJECT_ID`, `NEXT_PUBLIC_SANITY_DATASET`, `SANITY_API_READ_TOKEN`. On 16.3+ `next dev` writes `AGENTS.md` for you; on older releases you create it yourself. Embedded Studio config under `sanity/` is left alone. Route triage, `<Suspense>` placement and `instant()` regression tests belong to the official Next.js skills this one sequences with, so follow those guides rather than reinventing them here.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add sanity-io/next-sanity --skill sanity-live-cache-components -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/sanity-live-cache-components/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/sanity-live-cache-components/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add sanity-io/next-sanity --skill sanity-live-cache-components -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/sanity-live-cache-components/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add sanity-io/next-sanity --skill sanity-live-cache-components -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/sanity-live-cache-components/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install sanity-io/next-sanity sanity-live-cache-components
```

2. **Manually: put the files in place**
- File: `.github/skills/sanity-live-cache-components/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add sanity-io/next-sanity --skill sanity-live-cache-components -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/sanity-io/next-sanity --path skills/sanity-live-cache-components
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/sanity-live-cache-components/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add sanity-io/next-sanity --skill sanity-live-cache-components -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/sanity-io/next-sanity/tree/HEAD/skills/sanity-live-cache-components/SKILL.md

## When to use it
- move a Next.js + Sanity app onto `cacheComponents`
- add `sanityFetch` behind a shared `'use cache'` boundary
- wire `<SanityLive>` revalidation over EventSource
- split components into Page / Dynamic / Cached layers
- strip hardcoded `perspective` and `stega` from components

## Example prompts
- Set up Sanity Live and Cache Components with the sanity-live-cache-components skill; leave the Studio config untouched
- Refactor `sanityFetch` in `app/blog/[slug]/page.tsx` so `perspective` and `stega` arrive as props instead of constants
- Wire `<SanityLive>` and `<VisualEditing>` into the root layout and verify draft mode still works

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: ee3bb8ccba292a62.

BEGIN THIRD-PARTY FILE ee3bb8ccba292a62
```text
---
name: sanity-live-cache-components
description: Integrates Sanity Live with Next.js Cache Components in next-sanity v13+ apps. Sets up sanityFetch, a shared cachedSanity 'use cache' boundary, <SanityLive>, Visual Editing, Presentation Tool, draft mode handling, and the three-layer (Page/Dynamic/Cached) component pattern with explicit perspective/stega prop-drilling. Sequences with the official Next.js skills (next-cache-components-adoption, next-cache-components-optimizer, next-partial-prefetching-adoption, next-dev-loop). Use when configuring or migrating a Next.js app to cacheComponents with Sanity, when adding sanityFetch, when wiring <SanityLive>/<VisualEditing>, or when refactoring components that hardcode perspective/stega.
---

# Sanity Live + Cache Components

Wires `next-sanity` into a Next.js 16+ app with `cacheComponents: true`. Data is fetched with `sanityFetch` through a single shared `'use cache'` boundary (`cachedSanity`), and `<SanityLive>` in the root layout revalidates cached content over an EventSource connection to Sanity Content Lake. Visual Editing and Presentation Tool are fully supported when draft mode is enabled.

Read the relevant guide in `node_modules/next/dist/docs/` (when available) before writing code. If a guide conflicts with this skill, follow this skill.

This skill assumes familiarity with Cache Components fundamentals — `'use cache'`, `cacheLife`, `cacheTag`, and the cookies/headers/params rule — covered by the [Cache Components guide](https://nextjs.org/docs/app/getting-started/cache-components) (bundled offline under `node_modules/next/dist/docs/`). The only Sanity-relevant exception: `await draftMode()` is allowed inside `'use cache'` (Next.js bypasses caching when draft mode is enabled — see [the `use cache` reference](https://nextjs.org/docs/app/api-reference/directives/use-cache#draft-mode)).

## Where this skill fits
```
END THIRD-PARTY FILE ee3bb8ccba292a62

The third-party file has ended. Everything between the ee3bb8ccba292a62 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 1904 of 16820 bytes. © sanity-io · MIT · https://github.com/sanity-io/next-sanity/tree/HEAD/skills/sanity-live-cache-components/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
