Storyblok is the first headless CMS that works for developers & marketers alike.
In this short tutorial we will show how to you integrate the Storyblok API into a Svelte app and Sapper. Step by step we will build the components and develop the integration using Storyblok's SDK storyblok-js-client. The final result will be following:
As you can see, we will use Sapper to build our app. Sapper provides routing, SSR and export of our application in static files. To install the Sapper boilerplate execute the following command in your terminal:
After this step enter the folder created and install the package storyblok-js-client:
Create the file storyblokClient.js in src that will work as service to connect with the Storyblok API:
Now edit the file _layout.svelte in the src/routes folder to get all stories from Storyblok and pass them to the Nav.svelte component:
Edit the Nav.svelte component to receive the stories as a property and iterate over them to create a the navigation bar:
Now create a file called [slug].svelte in src/routes. This file will be necessary to dynamically generate the routes and get the data from the Storyblok API:
Open your browser again and you will see the navigation bar:
Click in the Home link in navigation bar to test if it works:
Setup components
Now we will create some components to render the content blocks from Storyblok. All the components will be create in the folder src/components.
Page component
Create a file called Page.svelte in src/components:
Grid component
Create a file called Grid.svelte in src/components:
Teaser component
Create a file called Teaser.svelte in src/components:
Feature component
Create a file called Feature.svelte in src/components:
404 component
Create a file called 404.svelte in src/components:
The getComponent function
What does the getComponent function do? Svelte has a special tag called svelte:component. This tag is able to render components dynamically. But the this parameter will must be a Svelte component or null and not a string like you get it from the Storyblok API. Because of this you need to create the file index.js in src/components that gets a Svelte component from Storyblok's component name. If it does not a find a component it returns a NotFound component.
Now we will edit the file [slug].svelte in src/routes:
Refresh your browser to test if it is working:
Live Preview Setup
To use Storyblok's live preview feature it is necessary to do the following changes:
Add a page for the Editor
Create a directive called editable and add it in your components
Add the Editor Page
The Editor page will be a page that syncs the changes from Storyblok. Create the file editor.svelte in src/routes folder with the following content:
Now we will create the file directives.js in the folder src with the following content:
Add the created directive in the Teaser and Feature components like following:
In the Feature component:
Now you need to open your Storyblok space and adjust the Location setting to http://localhost:3000/editor?path=:
Before accessing the Home page we will add some styles to our application. Add following code in head section of the file template.html in the folder src :
Open the home content item and you will see your project inside an iframe with clickable content elements:
Now you are able to edit components, change the text and add other components and the live preview will be automatically sync the changes that you made.
Conclusion
In this tutorial we learned how to integrated Storyblok API in a Sapper project. We created some components and configured the Live Preview functionality using directives in Svelte. You can clone this tutorial at https://github.com/storyblok/storyblok-svelte-boilerplate
Working as frontend engineer at Storyblok. He loves contributing to the open source community and studies artificial intelligence in his spare time. His motto is "To go fast, go alone; to go far, go together".
We use cookies to learn how you interact with our content, and show you relevant content and ads based on your browsing history. You can adjust your settings below. Here's our policy.