Sanity Integration
Sanity CMS provides the content management layer for Tune Me In, enabling rich editorial experiences that seamlessly reference Shopify products.Overview
Sanity stores:- Content Pages - Homepage, about pages, editorial articles
- Product References - Links to Shopify products with custom metadata
- Collections - Curated product groupings with editorial content
- Media Assets - Images with automatic optimization
- SEO Metadata - Titles, descriptions, and social sharing data
Configuration
Sanity is configured insanity.config.js:1-7:
shopify.config.js:8):
Using
useCdn: true enables Sanity’s global edge cache, providing fast content delivery worldwide. Set to false if you need real-time content updates during editing.The hydrogen-plugin-sanity Package
Tune Me In uses thehydrogen-plugin-sanity package (v0.1.1) to integrate Sanity with Hydrogen. This plugin provides:
useSanityQuery Hook
The primary interface for fetching data from Sanity with automatic Shopify product resolution:Key Features
- Automatic Product Detection - Scans Sanity responses for product references
- GraphQL Integration - Fetches referenced products from Shopify Storefront API
- Normalized Response - Returns products in an easy-to-use object map
- Customizable Fragments - Control which Shopify fields to fetch
Document Types
While the Studio schema lives in a separate repository, Tune Me In queries several document types:Product References
Products are referenced using theproductWithVariant pattern (fragments/productWithVariant.js:3-11):
- References the product document (
product->) - Checks availability status
- Includes the product slug for routing
- Resolves the selected variant (or defaults to first variant)
Homepage (_id: 'home3')
The homepage document includes:
Collections
Collection documents frompages/collections/[handle].server.jsx:94-105:
Collections support pagination using GROQ’s array slicing syntax
[$start..$end].Articles
Frompages/[handle].server.jsx:53-66:
GROQ Queries
Sanity uses GROQ (Graph-Relational Object Queries) as its query language.Basic Syntax
*- Select all documents[...]- Filter condition_type- Built-in field for document type&&- Logical AND operator
Projections
{}- Project specific fields"customField"- Rename fields in response->- Follow references[]- Array operator
Common Patterns
Filtering by slug:Portable Text
Portable Text is Sanity’s rich text format, stored as structured JSON.Structure
Fromfragments/portableText.js:8-52, the Portable Text fragment includes:
Custom Block Types
blockImage- Embedded images with captionsblockProduct- Full product cards inlineblockInlineProduct- Products within text flowblockInlineProductMarginalia- Products in marginsannotationProduct- Product links within text
Portable Text allows products to be referenced at multiple levels: as blocks, inline elements, or as annotations on text spans.
Image Handling
Images are stored in Sanity with metadata and transformations:Image Fragment
Fromfragments/image.js:13-20:
- Alt text for accessibility
- LQIP (Low Quality Image Placeholder) for progressive loading
- Dimensions for aspect ratio calculations
- URL for the full-resolution image
Image Transformations
Images can be transformed via URL parameters:w- Widthh- Heightfit- Fit mode (clip, crop, fill, etc.)q- Quality (1-100)fm- Format (jpg, png, webp, etc.)
Sanity Connect for Shopify
While not visible in the code, Sanity Connect is a crucial part of the architecture:- Automatic Sync - Syncs Shopify products to Sanity as documents
- Two-Way References - Products in Sanity maintain references to Shopify IDs
- Webhook Updates - Product changes in Shopify trigger Sanity updates
- Custom Fields - Add editorial fields to product documents
Sanity product documents use the ID format
shopifyProduct-{shopifyId}, which is how useSanityQuery identifies products to fetch from Shopify.Fragment Composition
Reusable GROQ fragments keep queries DRY and maintainable:Available Fragments
IMAGE- Standard image fieldsPRODUCT_WITH_VARIANT- Product reference with variantPORTABLE_TEXT- Rich text with all custom blocksSEO- SEO metadata fieldsLINKS- Internal and external linksPRODUCT_PAGE- Complete product page dataCOLLECTION_PAGE- Complete collection page data
Usage Example
Best Practices
Query Optimization
- Select only needed fields - Don’t use
...unless necessary - Use CDN for static content -
useCdn: truefor published content - Paginate large lists - Use array slicing for collections
- Cache results - Consider caching strategies for frequently accessed content
Product References
- Always check availability - Products can be deleted or deactivated
- Handle missing variants - Default to first variant if selected variant is unavailable
- Use normalized IDs - Products are keyed as
shopifyProduct-{id}
Content Modeling
- Reuse fragments - Keep GROQ queries consistent
- Structure for reuse - Design documents to support multiple contexts
- Plan for growth - Consider pagination and performance early
Next Steps
Shopify Integration
Learn how Shopify products are fetched and displayed
Data Fetching
Deep dive into the useSanityQuery hook
Architecture
Understand the overall system architecture