跳到正文
cccskills
免费在 GitHub 公开

sanity-data-operations

Manage Sanity content -- schema changes, data migrations, debugging queries, batch operations, and revalidation.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md5.5 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Sanity Data Operations

When to use this skill

  • Modifying Sanity schemas or adding new document types
  • Debugging missing or incorrect data in the frontend
  • Running batch operations on Sanity documents
  • Troubleshooting revalidation and ISR issues
  • Managing Sanity webhooks and live preview
  • Seeding or resetting demo data

Sanity project reference

FieldValue
Project IDnamias-cms (from NEXT_PUBLIC_SANITY_PROJECT_ID)
Datasetproduction (from NEXT_PUBLIC_SANITY_DATASET)
Studio URLhttp://localhost:3333 (local) / https://namias-cms.sanity.studio (prod)
API version2024-01-01

Workflow

1. Schema inspection

List all document types:

cd studio && npx sanity schema list

View a specific schema:

cd studio && npx sanity schema describe <documentType>

Common document types in this project:

TypePurpose
profileHero section: name, title, role rotator, email, image
projectPortfolio projects with title, slug, summary, tech, images
experienceWork history: role, company, dates, highlights
certificationCredentials: issuer, dates, expiry, credential URL
blogPostBlog articles with Portable Text body, categories
socialLinkSocial media links displayed in Connect section
skillSkills displayed in the Skills section
siteSettingsGlobal site configuration

2. Debugging data issues

Check if data exists via API:

curl "https://namias-cms.api.sanity.io/v2024-01-01/data/query/production?query=*[_type=='profile'][0]{name,email}"

Common query patterns:

// All projects
*[_type == "project"] | order(order asc) { title, slug, summary }

// Single project by slug
*[_type == "project" && slug.current == $slug][0]

// Published documents only (exclude drafts)
*[_type == "project" && !(_id in path("drafts.**"))]

// Count documents
count(*[_type == "blogPost"])

Debug in Sanity Studio:

  1. Open http://localhost:3333
  2. Go to Vision tab (GROQ playground)
  3. Run the query above
  4. Check that documents exist and have the expected fields

3. Data mutations

Using Sanity CLI:

cd studio && npx sanity document create <type>
cd studio && npx sanity document edit <documentId>
cd studio && npx sanity document delete <documentId>

Using GROQ mutations (via API):

curl -X POST "https://namias-cms.api.sanity.io/v2024-01-01/data/mutate/production" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{
    "mutations": [{
      "patch": {
        "id": "<documentId>",
        "set": { "title": "New Title" }
      }
    }]
  }'

4. Revalidation and ISR

How revalidation works:

  • The Sanity webhook calls /api/sanity/revalidate when content changes
  • The route uses revalidateTag() to invalidate cached data
  • SWR in components refetches on the next client visit

Trigger manual revalidation:

curl -X POST "http://localhost:3000/api/sanity/revalidate" \
  -H "Content-Type: application/json" \
  -d '{"secret": "<SANITY_REVALIDATE_SECRET>"}'

Check revalidation is working:

  1. Change content in Sanity Studio
  2. Open the frontend
  3. The change should appear within the revalidate period (or on manual trigger)

Common revalidation issues:

SymptomCauseFix
Changes don't appearWebhook not configuredCheck Sanity webhook settings
Stale data persistsTag not invalidatedCheck revalidateTag() calls in route handlers
401 on revalidateWrong secretVerify SANITY_REVALIDATE_SECRET env var
Build shows old dataISR cacheRedeploy or wait for revalidation period

5. Webhook configuration

Sanity webhook setup:

  1. Go to Sanity Management → Project → API → Webhooks
  2. Create a webhook:
    • URL: https://<your-domain>/api/sanity/revalidate
    • Trigger on: create, update, delete
    • Filter: _type in ["project", "experience", "certification", "blogPost", "profile"]
    • Secret: match SANITY_REVALIDATE_SECRET

6. Seeding demo data

npm run sanity:seed

This runs scripts/sanity/seed-demo.ts which creates sample documents for development.

7. Common tasks

Add a new field to an existing document:

  1. Edit the schema in studio/schemas/<documentType>.ts
  2. Add the field definition
  3. Run cd studio && npx sanity typegen generate (if using typegen)
  4. Migrate existing documents if the field is required
  5. Test in studio at localhost:3333

Add a new document type:

  1. Create schema in studio/schemas/<newType>.ts
  2. Register in studio/schemas/index.ts
  3. Add to structureDeskTool in studio/deskStructure.ts
  4. Create a query in the frontend to fetch it
  5. Create a component to display it

Common mistakes

  • Editing documents directly in the production Sanity project without testing locally first
  • Forgetting to set revalidate on fetch calls ({ next: { revalidate: 60 } })
  • Using stale GROQ queries after schema changes
  • Not handling null responses from Sanity (always use ?? defaultValue)

Delivery checks

  • Schema changes work in Sanity Studio locally
  • Frontend renders the new/updated data correctly
  • Revalidation webhook triggers and data refreshes
  • No TypeScript errors after schema changes
  • Draft documents are filtered out in production queries

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Audit and fix accessibility issues across the portfolio to ensure WCAG 2.1 AA compliance.

中文简介正在准备中。当前显示原文说明。

PP-Namias/Portfolio22026年10月22日 更新

Creates a new blog post document in the Sanity studio. Use when the user wants to write a blog post, publish an article, or schedule a future post.

中文简介正在准备中。当前显示原文说明。

PP-Namias/Portfolio22026年10月22日 更新

Creates a new certification document in the Sanity studio. Use when the user wants to add a credential, certificate, or professional accreditation to the Certifications collection.

中文简介正在准备中。当前显示原文说明。

PP-Namias/Portfolio22026年10月22日 更新

Adds a new skill MD file to the studio's skills collection. Use when the user wants to author a new step-by-step recipe for the studio's in-app Skills tool or the .agents mirror.

中文简介正在准备中。当前显示原文说明。

PP-Namias/Portfolio22026年10月22日 更新

Creates a new project document in the Sanity studio. Use when the user wants to add a new portfolio project, a case study, or a work sample to the Projects collection.

中文简介正在准备中。当前显示原文说明。

PP-Namias/Portfolio22026年10月22日 更新

Creates a new experience document in the Sanity studio. Use when the user wants to add a job, role, internship, or work history entry to the Experience collection.

中文简介正在准备中。当前显示原文说明。

PP-Namias/Portfolio22026年10月22日 更新

查看 PP-Namias 的全部技能

报告此技能的问题