sanity-best-practices
sanity-io/agent-toolkit
Comprehensive best practices for Sanity schema design, GROQ queries, Visual Editing, and framework integrations.
What is sanity-best-practices?
Official Sanity development best practices covering schema design, GROQ optimization, TypeGen, Visual Editing, Portable Text, Sanity Functions, Blueprints, and integrations with Next.js, Nuxt, Astro, Remix, SvelteKit, Angular, and Hydrogen. Use this when building or reviewing Sanity projects, designing content models, or implementing frontend integrations.
- Schema design and field definition patterns with validation and deprecation guidance
- GROQ query optimization, type safety, and performance best practices
- Visual Editing and Presentation Tool setup with live preview and Stega overlays
- Framework integration guides for Next.js, Nuxt, Astro, Remix, SvelteKit, Angular, and Hydrogen
- Sanity Functions and Blueprints for content automation and infrastructure as code
- Studio structure configuration, localization patterns, and content migration strategies
How to install sanity-best-practices
npx skills add https://github.com/sanity-io/agent-toolkit --skill sanity-best-practices- Sanity project initialized and CLI installed
- Basic familiarity with Sanity Studio and content modeling
How to use sanity-best-practices
- 1.Identify the relevant topic or framework guide from the Quick Reference section
- 2.Read the corresponding reference file for detailed explanations and code examples
- 3.Apply the patterns and best practices to your schema, queries, or integration
- 4.Reference the Global Rules for document ID generation and relationship modeling
- 5.Consult additional guides only when your task crosses multiple concerns
Use cases
- Setting up a new Sanity project with proper schema and Studio structure
- Integrating Sanity with a Next.js or other frontend framework for live content editing
- Optimizing GROQ queries for performance and type safety in production
- Implementing Visual Editing with the Presentation Tool and live preview
- Automating content workflows with Sanity Functions and event handlers
- Sanity developers building new projects or maintaining existing codebases
- Frontend engineers integrating Sanity with Next.js, Nuxt, Astro, or other frameworks
- Content architects designing schemas and content models
- DevOps engineers managing Sanity infrastructure with Blueprints
- Teams implementing content automation with Sanity Functions
sanity-best-practices FAQ
Let Sanity generate _id values for ordinary documents. Use explicit IDs mainly for singleton documents controlled by Studio Structure, including localized singletons like homePage-en.
Use reference fields to model relationships, then resolve related documents with GROQ lookups, source-key fields, or returned _id values from created documents.
Start with the single framework or topic guide that best matches your request (e.g., nextjs, nuxt, astro). Read additional references only when your task crosses multiple concerns.
Schema design, GROQ queries, TypeGen, Visual Editing, images, Portable Text, Studio structure, localization, migrations, Sanity Functions, Blueprints, and integrations with Next.js, Nuxt, Astro, Remix, SvelteKit, Angular, and Hydrogen.
Yes. The skill includes migration guides covering content import overview and HTML to Portable Text conversion with @portabletext/block-tools.
Full instructions (SKILL.md)
Source of truth, from sanity-io/agent-toolkit.
name: sanity-best-practices description: Sanity development best practices for schema design, GROQ queries, TypeGen, Visual Editing, images, Portable Text, Studio structure, localization, migrations, Sanity Functions, Blueprints, and framework integrations such as Next.js, Nuxt, Astro, Remix, SvelteKit, Angular, Hydrogen, and the App SDK. Use this skill whenever working with Sanity schemas, defineType or defineField, GROQ or defineQuery, content modeling, Presentation or preview setups, Sanity-powered frontend integrations, Sanity Functions, documentEventHandler, defineDocumentFunction, defineMediaLibraryAssetFunction, @sanity/functions, @sanity/blueprints, sanity.blueprint.ts, event-driven content automation, or when reviewing and fixing a Sanity codebase.
Sanity Best Practices
Comprehensive best practices and integration guides for Sanity development, maintained by Sanity. Use the quick reference below to load only the one or two topic files that match the task.
When to Apply
Reference these guidelines when:
- Setting up a new Sanity project or onboarding
- Integrating Sanity with a frontend framework (Next.js, Nuxt, Astro, Remix, SvelteKit, Hydrogen)
- Writing GROQ queries or optimizing performance
- Designing content schemas
- Implementing Visual Editing and live preview
- Working with images, Portable Text, or page builders
- Configuring Sanity Studio structure
- Setting up TypeGen for type safety
- Implementing localization
- Migrating content from other systems
- Building custom apps with the Sanity App SDK
- Managing infrastructure with Blueprints
- Automating content workflows with Sanity Functions
Global Rules
- Let Sanity generate
_idvalues for ordinary documents. Do not create deterministic UUIDs, slug-derived IDs, or legacy-system IDs when creating documents. - Model relationships with
referencefields, then resolve related documents with GROQ lookups, source-key fields, or returned_idvalues from created documents. - Use explicit document IDs mainly for singleton documents controlled by Studio Structure, including localized singletons such as
homePage-en.
Quick Reference
Integration Guides
get-started- Interactive onboarding for new Sanity projectsnextjs- Next.js App Router, Live Content API, standalone Studionuxt- Nuxt integration with @nuxtjs/sanityangular- Angular integration with @sanity/client, signals, resource APIastro- Astro integration with @sanity/astroremix- React Router / Remix integrationsvelte- SvelteKit integration with @sanity/svelte-loaderhydrogen- Shopify Hydrogen with Sanityproject-structure- Standalone Studio and monorepo patternsapp-sdk- Custom applications with Sanity App SDKblueprints- Infrastructure as Code with Sanity Blueprintsfunctions- Automating content workflows with Sanity Functions
Topic Guides
groq- GROQ query patterns, type safety, performance optimizationschema- Schema design, field definitions, validation, deprecation patternsvisual-editing- Presentation Tool, Stega, overlays, live previewpage-builder- Page Builder arrays, block components, live editingportable-text- Rich text rendering and custom componentsimage- Image schema, URL builder, hotspots, LQIP, Next.js Imagestudio-structure- Desk structure, singletons, navigationtypegen- TypeGen configuration, workflow, type utilitiesseo- Metadata, sitemaps, Open Graph, JSON-LDlocalization- i18n patterns, document vs field-level, locale managementmigration- Content import overview (see alsomigration-html-import)migration-html-import- HTML to Portable Text with @portabletext/block-tools
How to Use
Start with the single framework or topic guide that best matches the request, then read additional references only when the task crosses concerns. Use these reference files for detailed explanations and code examples:
references/groq.md
references/schema.md
references/nextjs.md
Each reference file contains:
- Comprehensive topic or integration coverage
- Incorrect and correct code examples
- Decision matrices and workflow guidance
- Framework-specific patterns where applicable
Related skills
More from sanity-io/agent-toolkit and the wider catalog.
seo-aeo-best-practices
SEO and AEO best practices for metadata, Open Graph, sitemaps, robots.txt, hreflang, JSON-LD structured data, EEAT, and content optimized for search engines and AI answer surfaces. Use this skill when implementing page SEO, technical SEO, schema markup, international SEO, AI-overview readiness, or improving content for Google, ChatGPT, Perplexity, and similar assistants.
sanity-migration
Plans, implements, and reviews migrations from other CMSes and content systems into Sanity. Use when migrating or replatforming to Sanity from AEM, Adobe Experience Manager, Contentful, Strapi, Webflow, WordPress, Payload, Drupal, Markdown/MDX/frontmatter files, WXR/XML exports, CMS APIs, database dumps, static HTML, or when designing extraction, transformation, Portable Text conversion, asset migration, redirects, validation, and cutover workflows.
content-modeling-best-practices
Structured content modeling guidance for schema design, content architecture, content reuse, references versus embedded objects, separation of concerns, and taxonomies across Sanity and other headless CMSes. Use this skill when designing or refactoring content types, deciding field shapes, debating reusable versus nested content, planning omnichannel content models, or reviewing whether a schema is too page-shaped or presentation-driven.
content-experimentation-best-practices
Content experimentation and A/B testing guidance covering experiment design, hypotheses, metrics, sample size, statistical foundations, CMS-managed variants, and common analysis pitfalls. Use this skill when planning experiments, setting up variants, choosing success metrics, interpreting statistical results, or building experimentation workflows in a CMS or frontend stack.
portable-text-serialization
Render and serialize Portable Text to React, Svelte, Vue, Astro, HTML, Markdown, and plain text. Use when implementing Portable Text rendering in any frontend framework, building custom serializers for non-standard block types, converting Portable Text to HTML strings server-side, converting Portable Text to Markdown, extracting plain text from Portable Text, or troubleshooting rendering issues with marks, blocks, lists, or custom types.
portable-text-conversion
Convert HTML and Markdown content into Portable Text blocks for Sanity. Use when migrating content from legacy CMSs, importing HTML or Markdown into Sanity, building content pipelines that ingest external content, converting rich text between formats, or programmatically creating Portable Text documents. Covers @portabletext/markdown (markdownToPortableText), @portabletext/block-tools (htmlToBlocks), custom deserializers, and the Portable Text specification for manual block construction.