Add a headless CMS to Astro in 5 minutes
Storyblok is the first headless CMS that works for developers & marketers alike.
In this tutorial, we’ll learn how to retrieve and display data from the Storyblok API in Astro in order to create a highly performant website with one of the most modern static site generators that exist.
Before moving on, if you’re unfamiliar with the concept of a Headless CMS or Storyblok in particular, you may consult the Storyblok Guide.
In a hurry? Check out the source code on GitHub and take a look at the live version on Netlify.
Environment Setup
Requirements
In order to follow this tutorial, please make sure you meet these requirements:
- Basic understanding of HTML and JavaScript
- Node.js LTS version installed
- An account in the Storyblok App
Create an Astro project
Let’s create a new Astro project by running the following command:
The wizard will guide us through the installation process. For this tutorial, we want to choose the following options:
- Where should we create your new project? Choose your desired project folder.
- How would you like to start your new project? Empty
- Install dependencies? Yes
- Do you plan to write TypeScript? No
- Initialize a new git repository? Up to you!
After the installation has completed, you can go to the project folder and run npm run dev
to start the development server.
Adding TailwindCSS
For the styling of our Astro website, we are going to employ Tailwind CSS. Luckily, adding Tailwind to Astro is a breeze.
All you have to run is the following command:
And that's it!
Create a new space in the Storyblok app by choosing the Create space {1} option. Pick a name for it {2}. Optionally, you can choose between different server locations for your space {3} (if you choose the United States or China, please be mindful of the required API parameter explained hereinafter).
Shortly afterward, a Storyblok space with sample content has been created for you. Let’s open the Home story by first clicking on Content {1} and then on Home {2}:
Now you’ll see the default screen and the Visual Editor:
Enabling the Visual Editor
In order to see your website in the Storyblok Visual Editor, the default environment URL has to be configured accordingly. To achieve that, simply go to Settings {1} > Visual Editor {2} and the Location {3} field to https://localhost:3000/
:
Storyblok v2 requires that your website is served via HTTPS. You can follow these instructions to set up your dev server accordingly.
Having done that, you can now go back to the Home story. However, you’ll notice that you won’t see the welcome screen of your Astro project yet.
Just one more step to go: open the Entry configuration {1} and set the Real path to /
{2}. Save the story and you’ll see the default screen of your Astro project in the Visual Editor.
Connecting Astro to Storyblok
First of all, we’ll have to install @storyblok/astro, the official SDK to interact with the Storyblok API.
Let’s add it to our project by running:
In order to access the data from your newly created space, you need to get your unique API token, which can be found in Settings {1} > Access tokens {2}. Here you can copy the Preview token {3} that's been generated for you.
You can store your preview access token in an .env
file in your project root like this:
In order to use environment variables in Astro, we quickly need to install Vite as a dependency:
Now we can use the token and initialize the Storyblok integration in astro.config.mjs
:
Setting the correct region
Depending on whether your space was created in the EU, the US, Australia, Canada, or China, you may need to set the region
parameter of the API accordingly:
eu
(default): For spaces created in the EUus
: For spaces created in the USap
: For spaces created in Australiaca
: For spaces created in Canadacn
: For spaces created in China
Here's an example for a space created in the US:
Note: For spaces created in any region other than the EU, the region parameter must be specified.
Creating a layout and an index page
Let’s use the useStoryblokApi
function we just created to fetch the data of the Home story. Simply replace the existing src/pages/index.astro
file with the following code:
As you can see, a component called StoryblokComponent
is imported from the SDK. This component is used to dynamically resolve all of the components that exist in our Storyblok space. Let’s take a closer look at this in the next step.
Moreover, we utilize a layout component. Let’s create it:
Learn more about Astro Layouts.
Rendering the Storyblok components
When you create a new Storyblok space and choose to start from scratch, the following four components are created automatically for you:
page
: Content type blockgrid
: Nestable blockfeature
: Nestable blockteaser
: Nestable block
You can find all of these in the Block Library of your space.
You can learn more about content types and nestable components in our Structures of Content tutorial.
In order to provide the frontend counterparts for these components in our Astro project, we, first of all, need to register them properly in the Storyblok integration. Let's change astro.config.mjs
accordingly:
It's crucial that the keys in the components
object match the technical names of the blocks defined in your Storyblok space.
As it is impossible to know in advance which nestable components or bloks will need to be rendered to display our story, StoryblokComponent.astro
provided by @storyblok/astro
loads the required components based on their technical name.
Having completed this step, we can now create the actual components and display the information that is being passed on in the blok
property object.
As you can see, both Page.astro
and Grid.astro
utilize StoryblokComponent.astro
to render any nested blocks in the body
or columns
fields respectively.
Have you noticed the storyblokEditable
function? It is required for the Storyblok JavaScript Bridge, allowing you to interact with your frontend components in the Visual Editor.
Finally, after having created all of these, you can launch your development server again by running npm run dev
. Now, if you go to the Home story in your Storyblok space, you should see your Teaser, Grid, and Feature components being rendered:
Limitations of using the Storyblok Bridge with Astro
Since one of the core ideas of Astro is to ship less JavaScript, its native components are not reactive. Therefore, real-time editing in the Storyblok Visual Editor as you may know from using Storyblok with a framework like Next.js won’t be possible.
However, what we can do is enable the functionality that clicking on components immediately shows the related content fields. Additionally, we can trigger a reload of our Astro frontend whenever the Save button in Storyblok is used.
The Storyblok Bridge is enabled by default for you. However, you can choose to disable it using the bridge
parameter. Learn more in the official SDK readme.
Continue reading and find out how to Render Storyblok Stories Dynamically in Astro.
Resource | Link |
---|---|
Astro | https://astro.build/ |
The Storyblok Astro Ultimate Tutorial | https://www.storyblok.com/tp/the-storyblok-astro-ultimate-tutorial/ |
Storyblok SDK for Astro | https://github.com/storyblok/storyblok-astro |
Storyblok SDK Live Demo | https://stackblitz.com/edit/astro-sdk-demo |
Storyblok in the Astro Docs | https://docs.astro.build/en/guides/cms/storyblok/ |
Building Future-Proof High-Performance Websites With Astro Islands And Headless CMS | https://www.smashingmagazine.com/2023/02/building-future-proof-high-performance-websites-astro-islands-headless-cms-storyblok/ |
Storyblok APIs | https://www.storyblok.com/docs/api |