Render Storyblok Stories Dynamically in Next.js 14
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, 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 -
Similarly, add the following code to your Footer.js
file -
Now we need to add these two components to our layout.js
file in the app directory. Replace the code of the file with the the following code -
Now, 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]] folder
To create all routes programmatically, we need to create a folder [[...slug]]
and a page.js
file inside it to dynamically generate routes for all stories inside Storyblok. Here the [[...slug]]
folder is used to catch all the routes. The page.js
file will contain the logic to render those pages. Since we are catching all the routes here, we can delete the page.js
file in the app directory for our index page (home slug). We will use the new file inside the new folder to handle everything.
You can read more about dynamic routes in Next.js here
Dynamic route generation with generateStaticParams
Next.js offers the generateStaticParams functionality to statically generate routes at build time instead of on-demand at request time. If you export an async
function called generateStaticParams
from a page that uses dynamic routes, Next.js will statically pre-render the paths depending on the logic inside the function. Add the following code to the page.js
file.
On this dynamic page generation file, the function generateStaticParams
reads all links from Storyblok(Line 25), 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 slug itself. Then we return the paths array. This format depends on the type of dynamic segments. You can read more about the returns here.
Finally, for every route that we return, we fetch the data from Storyblok(Line 12) inside the Page function. We also manage the home slug inside the Page function.
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.
Adding pages to Storyblok
To create a new story, we need to go to the Content
section {1} in our space. Once we are there, we will see a button Create New
{2} on the top right corner. Click on the button. We will see two options - one to create a new story and another one to create a new folder. Let’s click on Story
{3}
Once we click on it, we will get a popup with a couple of fields to create a new story. Let’s create an About
page. Fill the Name
{1} and Slug
{2} fields as in the image below -
Once we hit Create
we will get a new story added, and we will have a page with just our layout. Since we don't see a 404 here, this means the page is being rendered with the help of our new structure.
We can similarly create the Blog
and the Services
page as well. These stories are getting loaded with the new page.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 in 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.
Resource | Link |
---|---|
Storyblok Next.js Ultimate Tutorial | https://www.storyblok.com/tp/nextjs-headless-cms-ultimate-tutorial |
Storyblok Technologies Hub | https://www.storyblok.com/technologies |
Next.js Technology Hub | Storyblok Next.js Technology Hub |
Storyblok React SDK | storyblok/storyblok-react |