Sanity
Introduction
In this guide you will learn how to integrate Shopstory visual builder with Sanity CMS. All the code snippets are taken from our example project which you can find here: https://github.com/shopstory-app/shopstory-examples (examples/sanity-next-13
). We highly recommend installing the example project on an empty Sanity project to play around.
Important! Our examples repository is a monorepo that works correctly only with pnpm
. Please read README.md
before starting.
Getting started
Installation
The first step is installing Shopstory - Sanity integration package in your project.
Add Shopstory plugin
Shopstory is just a field in your schema. You can think of Shopstory as something similar to rich text - just way more advanced. You can add Shopstory anywhere within your content model.
It all starts with adding shopstory
plugin to the sanity.config.ts
:
Let's look at shopstory
parameters:
accessToken
- Shopstory access token required for authentication.canvasUrl
- the URL to the Shopstory canvas page you created in previous chapter. In the example code above we're hardcodingSITE_URL
constant value tohttp://localhost:3000
. In real-world scenario you will probably want it to come from environment variable (local and production URLs might differ).locales
- the list of locales accepted by Shopstory. Thefallback
parameter tells which locale should be displayed if there is no translation for the current one. In the example above ifde
is missing the translation then user will seeen
.isDefault
determines the root locale which is applied when fallback can't be found. Only one locale can be the default locale.assetSource
- this properly tells Shopstory which Sanity asset source should be used when selecting media from Shopstory. In this example we usemediaAssetSource
from the one and onlyhttps://www.sanity.io/plugins/sanity-plugin-media
Add Shopstory field to your schema
Let's create a new document type that represents a piece of Shopstory content. We'll name it shopstoryBlock
and it's available in src/schemas/shopstory-block.ts
:
That's all we need - just add shopstory
field to your content model.
Add Sanity plugin to the Shopstory config
We already added Shopstory plugin to Sanity, but we must also add Sanity plugin to Shopstory. Let's edit shopstory/config.ts
:
Just populate dataset
, projectId
and token
with your Sanity credentials.
Let's build some content
It's time to build something visually. Let's create a new shopstoryBlock
document and add some sections visually:
Displaying content
The last step is to render Shopstory content in your project. For the demo purpose we'll create a page that renders Shopstory Block entry by id - pages/shopstory-block/[entryId].tsx
:
Most of the code is CMS-agnostic and relates to how Shopstory SDK works (ShopstoryClient
, ShopstoryMetadataProvider
, Shopstory
, etc). This will be explained in the next chapter: Displaying Content.
However, there are 2 Sanity-specific pieces of code here.
First one is the body of the fetchShopstoryContentJSONFromCMS
function. As you can see it's a standard GROQ call to the Sanity API. In this example we fetch the field named shopstory
from our newly created shopstoryBlock
document.
The second Sanity-specific piece of code is here:
Here we tell the Shopstory-Sanity Plugin if the Sanity resources linked in Shopstory content (media, documents or files) should be fetched as drafts (preview: true
) or as published documents (preview: false
).
getStaticPaths
implementation is dummy just for the sake of demo simplicity.
Further reading
Now it's time to read Displaying Content chapter and understand how Shopstory SDK works.
Last updated