Render Storyblok Stories Dynamically in Next.js
Storyblok is the first headless CMS that works for developers & marketers alike.
In this short tutorial, we will see how to start making a real website with Next.js and Storyblok. We will add a layout that includes a navigation bar and a footer to our website to make it look better. We will also see how we can add new pages and render them dynamically according to the added components inside them.
If you’re in a hurry, have a look at our live demo in Stackblitz! Alternatively, you can explore or fork the code from the Next Ultimate Tutorial GitHub Repository.
Requirements
This is a part of the Ultimate Tutorial Guide for Next.js. You can find the previous part of the series here, which shows you how to integrate Storyblok and Next.js in 5 minutes. We recommend you take a look at that tutorial before starting this one.
We will be using the code from the 5 minutes tutorial as a starting point. You can find it here.
Adding a Layout
Let’s add a layout to our website by creating a static navigation bar and a footer. We will look at how to create dynamic navigation bars in an upcoming tutorial of this series.
Make two files in your components
folder, Navigation.js
and Footer.js
Add the following code to Navigation.js
file -
Make sure to replace the logos on lines 16 and 21 for desktop and mobile respectively. You can choose any images you like. You will also need to add an image for the mobile navigation bar in line 87.
Similarly, add the following code to your Footer.js
file -
Now we need to add a Layout
file in the components folder, which will use the two newly created components. Add the following code to the Layout.js
file -
Now, we need to add this layout to our index.js
file.
Replace the index.js
file code with the following -
After hitting save, our home story should look something like this -
Right now, we can see that we have three entries in the navigation menu - About, Blog, and Services. But we don’t have any stories created for them. So, if we try to go on any of those links from our browser, we will get 404 - This page could not be found
. Let’s add these stories to our space, but before that let's also see how to set up the dynamic route generation and catch all routes in Next.js so that we don't have to create files inside our Next.js app for every page we create inside Storyblok.
Catch all routes in Next.js: [...slug].js
To create all routes programmatically, we need to create a file pages/[...slug].js
to dynamically generate routes for all stories inside Storyblok.
Dynamic route generation with getStaticPaths
Next.js offers the getStaticPaths functionality to pre-render dynamic routes. If you export an async
function called getStaticPaths
from a page that uses dynamic routes, Next.js will statically pre-render all the paths specified by getStaticPaths
. Add the following code to the [...slug].js file.
On this dynamic page generation file, the function getStaticPaths
read all links from Storyblok(Line 47), this also includes draft links as we added the version to be draft. You can check what is returned from the links
endpoint by appending your preview token
to the following link: https://api.storyblok.com/v1/cdn/links?&version=draft&token=preview-token (Right now, we will just receive and see the home story in the response. But as we add more stories in Storyblok, we will see those as well.)
Then, we check each link that is returned from Storyblok, and it creates a page for every link except for any folder or the home story itself, since that is already created in our index.js
file.
Finally, we can read the slug of each page in getStaticProps
with params.slug.join('/)
(Line 27). Here the slug is an array because of the catch all rule in Next.js. Using that slug, we're requesting the correct entry from Storyblok.
It's also possible to make the catch-all routes optional by putting the ...slug
in double brackets: [[...slug]].js
. The main difference between catch-all and optional catch-all routes is that with optional, the route without the parameter is also matched.
In order not to have to change the real path & slugs constantly, it makes sense to already create the correct folder structure in Storyblok and use the catch all routes.
Now you can provide a name {1} – the slug will be filled out automatically for you. Let’s start with the About page.
Once we hit Create
we will get a new story added, and we will have a page with just our layout.
We can similarly create the Blog
and the Services
page as well. These stories are getting loaded with the […slug].js
file, which is catching the routes. Now, let’s also see how we can add the existing components to any of the pages.
On the right-hand side of the about story, we can see the empty body with a plus button that will allow us to add existing components to the page. If we click on it, we will see the following-
We have a list of existing components, and we can choose any of them. Let’s add the Teaser
component, and fill in the Name
field for that. As soon as we start filling the content in, we see the changes on the visual editor as well.
Once we hover on the right-hand side, we should see a similar plus button below the Teaser
. Let’s add a Grid
component along with three Features
as columns, similar to what we have on the home
story. We should see something like this now -
In a similar way, we can add any other components and they will be rendered automatically. We could add another Grid
and it should look something like this -
We can even add more components anywhere we like in the about story, if we have those created. Now, we could also add the components of choice to the two other stories, Blog
and Services
, as well.
Wrapping Up
In this tutorial, we saw how to start creating a real website after we set up a project using Storyblok and Next.js. Additionally, we learned how to create new stories and pages along with the dynamic rendering of the components wherever we place them.
You can find the next tutorial of this series to create a dynamic navigation bar and footer here.
After that tutorial, we will see how to create components with more fields and types from scratch along with extending the behaviour of the existing ones. You can find the tutorial here.
Resource | Link |
---|---|
Storyblok Next.js Ultimate Tutorial | https://www.storyblok.com/tp/nextjs-headless-cms-ultimate-tutorial |
Stackblitz Demo | https://stackblitz.com/edit/render-storyblok-stories-dynamically-in-next-js |
Storyblok Technologies Hub | https://www.storyblok.com/technologies |
Next.js Technology Hub | Storyblok Next.js Technology Hub |
Storyblok React SDK | storyblok/storyblok-react |