Sanity
Last updated
Last updated
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: (examples/sanity-next-13
). We highly recommend installing the example project on an empty Sanity project to play around.
The first step is installing Shopstory - Sanity integration package in your project.
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 hardcoding SITE_URL
constant value to http://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. The fallback
parameter tells which locale should be displayed if there is no translation for the current one. In the example above if de
is missing the translation then user will see en
. isDefault
determines the root locale which is applied when fallback can't be found. Only one locale can be the default locale.
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.
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.
It's time to build something visually. Let's create a new shopstoryBlock
document and add some sections visually:
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
:
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
).
assetSource
- this properly tells Shopstory which Sanity asset source should be used when selecting media from Shopstory. In this example we use mediaAssetSource
from the one and only
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: .
Now it's time to read chapter and understand how Shopstory SDK works.