• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    TypeScript
  • Created about 2 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Astro SDK for Storyblok CMS
Storyblok + Astro

@storyblok/astro

Astro integration for the Storyblok Headless CMS.


Storyblok Astro npm

Follow @Storyblok
Follow @Storyblok

Live Demo

If you are in a hurry, check out our official live demo on StackBlitz.

Usage

If you are first-time user of Storyblok, read the Getting Started guide to get a project ready in less than 5 minutes.

Installation

Install @storyblok/astro:

npm install @storyblok/astro
# yarn add @storyblok/astro
# See below for pnpm

Note
With pnpm, hoist Storyblok dependencies publicly with .npmrc. For more information, check pnpm documentation on here.

Add the following code to astro.config.mjs and replace the accessToken with the preview API token of your Storyblok space.

import { defineConfig } from "astro/config";
import storyblok from "@storyblok/astro";

export default defineConfig({
  integrations: [
    storyblok({
      accessToken: "<your-access-token>",
    }),
  ],
});

Warning
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.

Options

When you initialize the integration, you can pass all @storyblok/js options.

// Defaults
storyblok({
  accessToken: "<your-access-token>",
  bridge: true,
  apiOptions: {}, // storyblok-js-client options
  components: {},
  componentsDir: "src",
  enableFallbackComponent: false,
  customFallbackComponent: "",
  useCustomApi: false,
});

Note
By default, the apiPlugin from @storyblok/js is loaded. If you want to use your own method to fetch data from Storyblok, you can disable this behavior by setting useCustomApi to true, resulting in an optimized final bundle.

Region parameter

Possible values:

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

Full example for a space created in the US:

storyblok({
  accessToken: "<your-access-token>",
  apiOptions: {
    region: "us",
  },
});

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

Getting started

1. Creating and linking your components to the Storyblok Visual Editor

In order to link your Astro components to their equivalents you created in Storyblok:

First, you need to load them globally by specifying their name and their path in astro.config.mjs:

components: {
  page: "storyblok/Page",
  feature: "storyblok/Feature",
  grid: "storyblok/Grid",
  teaser: "storyblok/Teaser",
},

Note
The src folder is automatically added to the beginning of the path, so in this example your Astro components should be located here:

  • src/storyblok/Page.astro
  • src/storyblok/Feature.astro
  • src/storyblok/Grid.astro
  • src/storyblok/Teaser.astro

You can choose any other folder in the src directory for your Astro components.

Note If you prefer to use a different folder than src, you can specify one using the componentsDir option:

storyblok({
  componentsDir: "app",
});

Now, your Storyblok components can be located anywhere in the app folder, e.g. page: "storyblok/Page" for app/storyblok/Page.astro or page: "Page" for app/Page.astro.

For each component, use the storyblokEditable() function on its root element, passing the blok property that they receive:

---
import { storyblokEditable } from "@storyblok/astro";

const { blok } = Astro.props
---

<div {...storyblokEditable(blok)}>
  <h2>{blok.headline}</h2>
</div>

Finally, you can use the provided <StoryblokComponent> for nested components; it will automatically render them (if they have been registered globally beforehand):

---
import { storyblokEditable } from "@storyblok/astro";
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";

const { blok } = Astro.props
---

<main {...storyblokEditable(blok)}>
  {blok.body?.map(blok => {return <StoryblokComponent blok="{blok}" />})}
</main>

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

Using fallback components

By default, @storyblok/astro throws an error if a component is not implemented. Setting enableFallbackComponent to true bypasses that behavior, rendering a fallback component in the frontend instead. You can also use a custom fallback component by (for example) setting customFallbackComponent: "storyblok/MyCustomFallback".

Using partial hydration

If you want to use partial hydration with any of the frameworks supported by Astro, follow these steps:

  1. Install the official Astro integration for your desired framework
  2. Create an Astro component that serves as a wrapper and utilizes the most suitable client directive
  3. Create the actual component in Vue, Svelte, React or any other supported framework

For working examples, please refer to the Live Demo on Stackblitz.

2. Getting Storyblok Stories and using the Storyblok Bridge

Fetching one Story

Use the useStoryblokApi function to have access to an instance of storyblok-js-client:

---
import { useStoryblokApi } from "@storyblok/astro";
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";

const storyblokApi = useStoryblokApi();
const { data } = await storyblokApi.get("cdn/stories/home", {
  version: "draft",
});

const story = data.story;
---

<StoryblokComponent blok="{story.content}" />

Note
The available methods are described in the [storyblok-js-client] repository(https://github.com/storyblok/storyblok-js-client#method-storyblokget)

Dynamic Routing

In order to dynamically generate Astro pages based on the Stories in your Storyblok Space, you can use the Storyblok Links API and the Astro getStaticPaths() function similar to this example:

---
import { useStoryblokApi } from "@storyblok/astro";
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";

export async function getStaticPaths() {
  const storyblokApi = useStoryblokApi();

  const { data } = await storyblokApi.getAll("cdn/links", {
    version: "draft",
  });
  let links = data.links;
  links = Object.values(links);

  return links.map((link) => {
    return {
      params: { slug: link.slug },
    };
  });
}

const { slug } = Astro.params;

const storyblokApi = useStoryblokApi();

const { data } = await storyblokApi.get(`cdn/stories/${slug}`, {
  version: "draft",
});

const story = data.story;
---

<StoryblokComponent blok="{story.content}" />

Using the Storyblok Bridge

The Storyblok Bridge is enabled by default. If you would like to disable it or enable it conditionally (e.g. depending on the environment) you can set the bridge parameter to true or false in astro.config.mjs:

Note
Since Astro is not a reactive JavaScript framework and renders everything as HTML, the Storyblok Bridge will not provide real-time editing as you may know it from other frameworks. However, it automatically refreshes the site for you whenever you save or publish a story.

You can also provide a StoryblokBridgeConfigV2 configuration object to the bridge parameter.

bridge: {
  customParent?: string,
  preventClicks?: boolean, // Defaults to false.
  resolveRelations?: strings[],
  resolveLinks?: string
}
  • customParent is used to provide a custom URL for the Storyblok editor iframe.
  • preventClicks prevents the default behaviour of clicks when inside the Storyblok editor.
  • resolveRelations may be needed to resolve the same relations that are already resolved in the API requests via the resolve_relations parameter.
  • resolveLinks may be needed to resolve link fields.

Note
resolveRelations and resolveLinks will not have any effect in Astro, since the Storyblok Bridge is configured to reload the page. Thus, all the requests needed will be performed after the reload.

The provided options will be used when initializing the Storyblok Bridge. You can find more information about the Storyblok Bridge and its configuration options on the In Depth Storyblok Bridge guide.

If you want to deploy a dedicated preview environment with the Bridge enabled, allowing users of the Storyblok CMS to see their changes being reflected on the frontend directly without having to rebuild the static site, you can enable Server Side Rendering for that particular use case. More information can be found in the Astro Docs.

Rendering Rich Text

Note
While @storyblok/astro provides basic richtext rendering capabilities, for advanced use cases, it is highly recommended to use storyblok-rich-text-astro-renderer.

You can easily render rich text by using either the renderRichText function or the <RichTextRenderer /> component, both of which are included in @storyblok/astro. Use renderRichText, which only supports parsing and returning native HTML tags, if you are not embedding bloks in your rich text. Then you can use the set:html directive:

---
import { renderRichText } from "@storyblok/astro";

const { blok } = Astro.props

const renderedRichText = renderRichText(blok.text)
---

<div set:html="{renderedRichText}"></div>

Use the <RichTextRenderer /> component if you are embedding bloks in your rich text:

---
import RichTextRenderer from "@storyblok/astro/RichTextRenderer.astro";

const { blok } = Astro.props
---

<RichTextRenderer richTextData={blok.richtext} />

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

import { RichTextSchema, renderRichText } from "@storyblok/astro";
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/v4/source/schema.js

const { blok } = Astro.props;

const renderedRichText = renderRichText(blok.text, {
  schema: mySchema,
  resolver: (component, blok) => {
    switch (component) {
      case "my-custom-component":
        return `<div class="my-component-class">${blok.text}</div>`;
        break;
      default:
        return `Component ${component} not found`;
    }
  },
});

The same can be done with the <RichTextRenderer /> component by passing along the options via the richTextOptions prop:

---
import { RichTextSchema } from "@storyblok/astro";
import RichTextRenderer from "@storyblok/astro/RichTextRenderer.astro";

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/v4/source/schema.js

const { blok } = Astro.props;

const options = {
  schema: mySchema,
  resolver: (component, blok) => {
    switch (component) {
      case "my-custom-component":
        return `<div class="my-component-class">${blok.text}</div>`;
        break;
      default:
        return `Component ${component} not found`;
    }
  },
};
---

<RichTextRenderer richTextData={blok.richtext} richTextOptions={options} />

Note
Please be aware that the <RichTextRenderer /> component is not supported when using Astro v1. Make sure to use Astro v2 or v3.

API

useStoryblokApi()

Returns the instance of the storyblok-js-client.

The Storyblok JavaScript SDK Ecosystem

A visual representation of the Storyblok JavaScript SDK Ecosystem

Acknowledgements

A huge thank you goes to the Astro Team. In particular to Tony Sullivan, who has provided extraordinary support and made automagically rendering Storyblok components a reality.

Related Links

More 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. We use the Angular Convention to name the commits.

More Repositories

1

storyblok

Info about Storyblok
309
star
2

storyblok-nuxt

Storyblok Nuxt module
TypeScript
256
star
3

nextjs-multilanguage-website

Nextjs Multilanguage Website
JavaScript
144
star
4

nuxtjs-multilanguage-website

Nuxtjs Multilanguage Website
Vue
137
star
5

storyblok-js-client

Universal JavaScript client for Storyblok's API
TypeScript
125
star
6

storyblok-react

React SDK for Storyblok CMS
TypeScript
108
star
7

nuxtdoc

A setup to build beautiful documentation with Nuxt and Storyblok deployed on Netlify for everyone
Vue
104
star
8

awesome-storyblok

A curated list of awesome things related to Storyblok CMS. 😏
101
star
9

storyblok-design-system

Design System for the Storyblok UI.
TypeScript
86
star
10

storyblok-svelte

Svelte SDK for Storyblok CMS
Svelte
83
star
11

storyblok-vue

Vue.js SDK for Storyblok CMS
TypeScript
82
star
12

react-next-boilerplate

Nextjs Storyblok boilerplate
JavaScript
61
star
13

vue-nuxt-boilerplate

Nuxt.js Storyblok boilerplate
Vue
52
star
14

storyblok-docs

A Nuxt.js setup to generate an intuitive, readable and collaborative API documentation with copy and paste able examples
Vue
52
star
15

gatsby-source-storyblok

Gatsby source plugin for building websites using the Storyblok headless CMS as a data source.
JavaScript
43
star
16

bootstrap-nuxt-demo

Storyblok Demo Website using Nuxt
Vue
39
star
17

gatsby-storyblok-boilerplate

Gatsby starter template with Storyblok's headless cms and true preview
JavaScript
37
star
18

storyblok-php-client

Storyblok - PHP Client
PHP
31
star
19

storyblok-js

JavaScript SDK to connect Storyblok with your favourite framework that we don't have an official SDK for.
TypeScript
29
star
20

nuxtwebsite

A simple Nuxt.js setup to create websites with blog feature using Storyblok as CMS and Netlify to deploy it.
Vue
28
star
21

storyblok-demo-default

Vue
26
star
22

storyblok-cli

The repo for our Storyblok CLI
JavaScript
25
star
23

field-plugin

Create and deploy Storyblok Field Plugin
TypeScript
25
star
24

nuxt-ultimate-tutorial

Vue
25
star
25

storyblok-ruby-client

Storyblok ruby client for easy access of the content delivery api
Ruby
24
star
26

gridsome-source-storyblok

Official Storyblok source plugin for Gridsome
JavaScript
23
star
27

vuejs-boilerplate

Storyblok - JavaScript - VueJS Boilerplate
JavaScript
22
star
28

talk-from-zero-to-nextjs-hero

JavaScript
22
star
29

field-type-examples

A collection of field-types by the community
TypeScript
21
star
30

sveltekit-ultimate-tutorial

Ultimate Tutorial Series for Integrating Storyblok with SvelteKit projects
Svelte
19
star
31

getting-started

An example hello world project build with Nuxt.js / Next.js for the Storyblok.
JavaScript
18
star
32

tool-examples

A collection of tools to use with Storyblok by the community
JavaScript
18
star
33

next.js-13-boilerplate

JavaScript
17
star
34

wordpress-importer

A simple script for migrating content from WordPress to Storyblok.
JavaScript
17
star
35

storyblok-vuestorefront

Demo project for integration of Vue Storefront with Storyblok
Vue
16
star
36

storyblok-svelte-boilerplate

Code of the tutorial: Add a headless CMS with live preview to Svelte and Sapper in 5 minutes
JavaScript
16
star
37

gatsbyjs-multilanguage-website

Multilanguage website built with GatsbyJS and Storyblok
JavaScript
15
star
38

nextjs-bigcommerce-starter

TypeScript
15
star
39

nuxtjs

A NuxtJS Website with Storyblok as content source.
Vue
13
star
40

storyblok-gridsome-boilerplate-moon

Storyblok's Gridsome Boilerplate (Moon-Theme)
Vue
13
star
41

nuxt-storyblok-snipcart

Nuxt.js Storyblok Boilerplate with Snipcart integration
Vue
13
star
42

storyblok-markdown-richtext

A html/markdown to Storyblok richtext converter
JavaScript
13
star
43

storyblok-translations

Repository to contribute translations for app.storyblok.com
13
star
44

jekyll

Add a headless CMS to Jekyll
Ruby
12
star
45

storyblok-fieldtype

Tool for creating custom fieldtypes for Storyblok with babel, browserify and hotreload
Vue
12
star
46

storyblok-express-boilerplate

Node.js Express boilerplate
CSS
11
star
47

storyblok-workflow-app

Example Storyblok app
Vue
11
star
48

storyblok-angular-example

TypeScript
10
star
49

next.js-ultimate-tutorial

JavaScript
10
star
50

rails-boilerplate

Ruby on Rails Storyblok Starter Boilerplate
Ruby
10
star
51

big-commerce-nuxt-starter

A starter shop template for Big Commerce with Storyblok
Vue
9
star
52

storyblok-integration-field-starter

Custom integration field type starter kit for Storyblok
Vue
9
star
53

storyblok-python-client

Storyblok API library client for python
Python
9
star
54

storyblok-create-demo

Set up a modern framework app with Storyblok by running one command.
TypeScript
9
star
55

storyblok-nextjs-multilanguage-isr-ssg-ssr

JavaScript
8
star
56

nuxt-auth

Storyblok authentification module for the Nuxt.js
JavaScript
8
star
57

talk-blazingly-fast-nuxtjs

Vue
8
star
58

storyblok-react-native-starter

A Starter Kit for Storyblok with React Native (Expo)
JavaScript
8
star
59

silex-boilerplate

Storyblok - PHP - Silex Boilerplate
PHP
8
star
60

space-tool-plugins

TypeScript
8
star
61

storyblok-nuxt-beta

JavaScript
8
star
62

storyblok-gridsome-boilerplate

Storyblok starter boilerplate for Gridsome
Vue
8
star
63

default-datasources

Storyblok - Default Datasources for Single Choice's
7
star
64

remix-ultimate-tutorial

JavaScript
7
star
65

storyblok-react-example

Example of the integration of Storyblok in React
JavaScript
7
star
66

storyblok.github.io

Client Side Integration of the headless Storyblok CMS directly in a github page.
HTML
6
star
67

storyblok-nuxt-2

JavaScript
6
star
68

storyblok-ruby-richtext-renderer

This is Storyblok's rendering service to render html from the data of the richtext field type.
Ruby
6
star
69

storyblok-php-richtext-renderer

Storyblok PHP Richtext Renderer converts the RichText field into HTML
PHP
6
star
70

nodejs-boilerplate

Storyblok - NodeJS - Boilerplate
JavaScript
5
star
71

nextjs-persisted-query-graphql-example

Example of Next.js + Apollo + Storyblok GraphQL api with persisted queries
JavaScript
5
star
72

mui

TypeScript
5
star
73

django-boilerplate

Storyblok - Python - Django Boilerplate
Python
5
star
74

lambda-form-submission

Serverless Form Submission via AWS Lambda and Storyblok as Storage
HTML
5
star
75

storyblok-csv-import-example

An example on how to read a local CSV file and push it into Storyblok using the Management API.
JavaScript
5
star
76

lambda-crawler

Lambda Crawler which extracts content from websites and saves it to Amazon DynamoDB
JavaScript
4
star
77

storyblok-commerce

Commerce Interface and Modules
Vue
4
star
78

lambda-crawler-aws-elasticsearch

Lambda Crawler which extracts content from websites and saves it to AWS Elasticsearch
JavaScript
4
star
79

storyblok-vue-starter

Vue
4
star
80

astro-ultimate-tutorial

Astro
4
star
81

starter-template

DEPRECATED - Storyblok - Start Template
CSS
4
star
82

quickstart

πŸš€ Quickstart for Rendering Service
Liquid
4
star
83

cloudflare-next-example

A Next.js template application on how to use cloudflare pages for hosting applications built on Storyblok
JavaScript
4
star
84

sinatra-boilerplate

Ruby/Sinatra Example of a Storyblok Integration
CSS
4
star
85

storyblok-graphql-react-apollo

Demo project showcasing Storyblok's GraphQL api
JavaScript
4
star
86

storyblok-tool-example

Example of a Storyblok toolbar app
Vue
4
star
87

city-theme

City Theme for Storyblok
CSS
4
star
88

serverless-custom-app-starter

A starter repository with Nuxt.js to create a custom application in Storyblok
JavaScript
3
star
89

tutorial-websites-scraper

JavaScript
3
star
90

storyblok-react-starter

JavaScript
3
star
91

storyblok-vuejs-website

A demo website using Storyblok with Vuejs
CSS
3
star
92

example-scripts

Collection of example node.js or scripts for imports, exports or other kind of tasks one would like to perform with Storyblok.
JavaScript
3
star
93

gulp-blok

Storyblok - Gulp Plugin
JavaScript
3
star
94

storyblok-sync-script

JavaScript
2
star
95

quickstart-templates

JavaScript
2
star
96

storyblok-fieldtype-react

Storyblok fieldtype plugin with React thanks to Vuera
HTML
2
star
97

nextjs-personalization-demo

nextjs-personalization-demo
JavaScript
2
star
98

gatsby-storyblok-boilerplate-v2

Gatsby (v2) starter template with Storyblok's headless cms and true preview
CSS
2
star
99

grunt-blok

Storyblok - Grunt Plugin
JavaScript
2
star
100

storyblok-app-example

JavaScript
2
star