Render Storyblok Stories Dynamically in Gatsby.js
Storyblok is the first headless CMS that works for developers & marketers alike.
If you'd like to build a multilanguage website with Gatsby, please follow this tutorial instead. If you'd like to build a website without internationalization logic, please keep following the rest of this article and the Ultimate Tutorial articles.
In this short tutorial, we will see how to start making a real website with Gatsby.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 Ultimate Tutorial GitHub repo part 2 branch!
Requirements
This is a part of the Ultimate Tutorial Guide for Gastby.js. You can find the previous part of the series here, which shows you how to integrate Storyblok and Gatsby.js in 5 minutes. We recommend you take a look at that tutorial before starting this one.
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, header.js
and footer.js
Add the following code to header.js
file -
Make sure to replace the logos on lines 18 and 23 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 89.
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
and {storyblokEntry.full_slug}.js
files.
Replace the index.js
file code with the following -
Similarly, replace the code in the {storyblokEntry.full_slug}.js
file with the following code -
The {storyblokEntry.full_slug}.js file is being used to generate all the stories other than 'home'.
After saving your code, 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.
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. We might see again a 404 error here, but this is because we are only fetching the published links and generating their pages. Once we hit the Publish
button, we will have a page with just our layout.
If you still see your About page is not being rendered, hit the Refresh Data button from GraphiQL. If you haven't yet enabled the Refresh Data button on the GraphiQL, you can take a look at this tutorial.
Rendering Pages dynamically
After we create the new pages in the Storyblok space, let’s take care of the required logic on the frontend side.
We can similarly create the Blog
and the Services
page. These stories are getting generated by the {storyblokEntry.full_slug}.js
file, which is catching all 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 Gatsby.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.
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 Gatsby.js Ultimate Tutorial | https://www.storyblok.com/tp/storyblok-gatsby-ultimate-tutorial |
Boilerplate Repository | https://github.com/storyblok/gatsby-storyblok-boilerplate |
Storyblok Technologies Hub | https://www.storyblok.com/technologies |
Gatsby.js Technology Hub | https://www.storyblok.com/tc/gatsbyjs |
Storyblok Gatsby SDK | https://github.com/storyblok/gatsby-source-storyblok |