Storyblok Logo

@storyblok/svelte

The Svelte SDK you need to interact with Storyblok API and enable the Real-time Visual Editing Experience.


npm package

Follow @Storyblok

Try @Storyblok

Kickstart a new project

Are you eager to dive into coding? Follow these steps to kickstart a new project with Storyblok and Svelte, and get started in just a few minutes!

Ultimate Tutorial

Are you looking for a hands-on, step-by-step tutorial? The SvelteKit Ultimate Tutorial has you covered! It provides comprehensive instructions on building a complete, multilingual website using Storyblok and SvelteKit from start to finish.

Compatibility

Version to install Support
Latest (from v3) @storyblok/svelte Modern browsers + Node 18+
Latest (from v3) @storyblok/svelte
+ Fetch polyfill like isomorphic-fetch
Browsers and Node versions with no Fetch API support
Version 2 @storyblok/svelte@2 Internet Explorer support

Usage

@storyblok/svelte helps you connect your Svelte project to Storyblok by:

Installation

Install @storyblok/svelte

npm install @storyblok/svelte

Please note that you have to use npm - unfortunately, we are currently not supporting yarn or pnpm for this SDK.

Initialize the library in your application by adding the apiPlugin and the access token of your Storyblok space:

import App from "./App.svelte";
import { storyblokInit, apiPlugin } from "@storyblok/svelte";

storyblokInit({
  accessToken: "<your-token>",
  // bridge: false,
  use: [apiPlugin],

  components: {
    teaser: Teaser,
  },
});

The best place to initialize the Storyblok library is in the main.ts. or main.js file if you are using svelte or in the load() function in the src/routes/+layout.js file if you are using SvelteKit.

List all your components to the components object in the storyblokInit function. You can load all of them by adding them to the list.

Region parameter

Possible values:

  • eu (default): For spaces created in the EU
  • us: For spaces created in the US
  • ap: For spaces created in Australia
  • ca: For spaces created in Canada
  • cn: For spaces created in China

Full example for a space created in the US:

  {
    accessToken: "<your-access-token>",
    apiOptions: {
      region: "us"
    }
  }

Note For spaces created in the United States or China, the region parameter must be specified.

Getting started

1. Fetching Content

Use the getStoryblokApi()` to get your stories from the Storyblok CDN API:

<script>
  import { onMount } from "svelte";
  import { getStoryblokApi } from "@storyblok/svelte";
  onMount(async () => {
    const storyblokApi = getStoryblokApi();
    const { data } = await storyblokApi.get("cdn/stories/home", {
      version: "draft",
    });
  });
</script>

Note You can skip using storyblokApi if you prefer your own method or function to fetch your data.

2. Listen to Storyblok Visual Editor events

Use useStoryBridge to get the updated story every time a change event is triggered from the Visual Editor. You need to pass the story id as the first param, and a callback function as the second param to update the new story:

<script>
  import { onMount } from "svelte";
  import { getStoryblokApi, useStoryblokBridge } from "@storyblok/svelte";
  let story = null;
  onMount(async () => {
    const storyblokApi = getStoryblokApi();
    const { data } = await storyblokApi.get("cdn/stories/home", {
      version: "draft",
    });
    story = data.story;
    useStoryblokBridge(story.id, (newStory) => (story = newStory));
  });
</script>

You can pass Bridge options as a third parameter as well:

useStoryblokBridge(story.id, (newStory) => (story = newStory), {
  resolveRelations: ["Article.author"],
  resolveLinks: "url",
});

To link the Storyblok components, you have to

  • Load them in components when calling storyblokInit

  • Use the storyblokEditable action on the root element of each component

<div use:storyblokEditable={blok} / >
  • Use the StoryblokComponent to load them by passing the blok property
<StoryblokComponent {blok} />

The blok is the actual blok data coming from Storyblok's Content Delivery API.

Features and API

You can choose the features to use when you initialize the plugin. In that way, you can improve web performance by optimizing your page load and saving some bytes.

Storyblok API

You can use an apiOptions object. This is passed down to the storyblok-js-client config object:

storyblokInit({
  accessToken: "<your-token>",
  apiOptions: {
    //storyblok-js-client config object
    cache: { type: "memory" },
  },
  use: [apiPlugin],
});

If you prefer to use your own fetch method, just remove the apiPlugin and storyblok-js-client won't be added to your application. You can find out more about our Content Delivery API in the documentation.

Storyblok Bridge

You can conditionally load it by using the bridge option. Very useful if you want to disable it in production:

storyblokInit({
  bridge: process.env.NODE_ENV !== "production",
});

Keep in mind you have still access to the raw window.StoryblokBridge:

const sbBridge = new window.StoryblokBridge(options);
sbBridge.on(["input", "published", "change"], (event) => {
  // ...
});

For background information on the Storyblok JS Bridge, please check out the documentation.

Rendering Rich Text

You can easily render rich text by using the renderRichText function that comes with @storyblok/svelte and Svelte {@html htmlstring} directive.

<script>
  import { renderRichText } from "@storyblok/svelte";
  export let blok;
  $: articleHTML = renderRichText(blok.article);
</script>

<div class="prose">{@html articleHTML}</div>

If you are allowing the content editors to add Blok (components like teaser, hero etc) into the rich text editor, you can also implement a specific logic for rendering correctly the components added to the rich text editor. To achieve this, you have to obtain the schema definition from the RichTextSchema via cloning object. For cloning schema you can use the clone-deep library for example:

npm i clone-deep

You can set a custom Schema and component resolver globally at init time by using the richText init option:

import { RichTextSchema, storyblokInit } from "@storyblok/svelte";
import cloneDeep from "clone-deep";
const mySchema = cloneDeep(RichTextSchema); // you can make a copy of the default RichTextSchema
// ... and edit the nodes and marks, or add your own.
// Check the base RichTextSchema source here https://github.com/storyblok/storyblok-js-client/blob/master/source/schema.js
storyblokInit({
  accessToken: "<your-token>",
  richText: {
    schema: mySchema,
    resolver: (component, blok) => {
      switch (component) {
        case "my-custom-component":
          return `<div class="my-component-class">${blok.text}</div>`;
        default:
          return "Resolver not defined";
      }
    },
  },
});

You can also set a custom Schema and component resolver only once by passing the options as the second parameter to renderRichText function:

<script>
  import {
    RichTextSchema,
    storyblokEditable,
    StoryblokComponent,
    renderRichText,
  } from "@storyblok/svelte";
  import cloneDeep from "clone-deep";
  const mySchema = cloneDeep(RichTextSchema);
  export let blok;
  $: resolvedRichText = renderRichText(blok.richText, {
  schema: mySchema,
  resolver: (component, blok) => {
    switch (component) {
      case "teaser":
        console.log(blok); // for learning purpose
        return `<div class="my-component-class">${blok.headline}</div>`;
        break;
      default:
        return `Component ${component} not found`;
    }
  },
});
</script>

Enabling SSL

For security reasons the Storyblok UI loads and embeds the frontend project that you are building with Svelte, via HTTPS protocol into Storyblok Visual Editor.

To enable the HTTPS protocol when you run npm run dev, you can install the basicSsl Vite plugin.

npm i @vitejs/plugin-basic-ssl

and, in the vite.config.js file be sure to load correctly the plugin, importing the plugin:

import basicSsl from '@vitejs/plugin-basic-ssl'

and then, activating the plugin in plugins configuration array:

    plugins: [sveltekit(), basicSsl()],

Compatibility

This plugin is for Svelte. Thus, it supports the same browsers as Svelte 3. In short: all modern browsers and IE10+.

Troubleshooting

Working with a Component Library

When working with a component library, create an alias pointing '@storyblok/svelte' to './node_modules/@storyblok/svelte'. This will ensure the imported module will use the local version of Storyblok SDK. In your vite.config.js, include:

import { sveltekit } from "@sveltejs/kit/vite";
import basicSsl from "@vitejs/plugin-basic-ssl";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);

const __dirname = path.dirname(__filename);

/** @type {import('vite').UserConfig} */
const config = {
  plugins: [sveltekit(), basicSsl()],
  server: {
    https: true,
  },
  resolve: {
    alias: {
      "@storyblok/svelte": path.resolve(
        __dirname,
        "./node_modules/@storyblok/svelte"
      ),
    },
  },
};

export default config;

Another option might also be using npm / yarn workspaces.

A note about Fetch API

⚠️ This SDK uses the Fetch API under the hood. If your environment doesn't support it, you need to install a polyfill like isomorphic-fetch. More info on storyblok-js-client docs.

The Storyblok JavaScript SDK Ecosystem

Further Resources

Support

Contributing

Please see our contributing guidelines and our code of conduct. This project uses semantic-release for generating new versions by using commit messages and we use the Angular Convention to name the commits. Check this question about it in semantic-release FAQ.

Top categories

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes