Getting Started with Next.js Commerce and Storyblok
Storyblok is the first headless CMS that works for developers & marketers alike.
In this tutorial, we will implement Storyblok into a Next.js commerce site. Next.js Commerce is a front-end starter kit for your e-commerce site. For this tutorial, we will use BigCommerce as our eCommerce platform.
Starting point
We are going to follow this guide from BigCommerce. It takes you to the step-by-step guild on creating and deploying the site to Vercel. This will be our starting point and we will have a fully working e-commerce site with dummy data.
If you already have an existing Next.js commerce site you can still follow this guide and learn how you can implement Storyblok in your site.
Now we can just clone the project from our own GitHub repo to our local machine to work on it.
You can quickly get all the env
variables from Vercel to your local machine by using the Vercel CLI: vercel env pull
Once the project is cloned locally do the following to run the project.
Create a Storyblok project
Sign up for a free Storyblok account. Once you're signed up you can create a new space.
Once the project is created, we can connect our Storyblok space to our project. You can also follow our Next.js starter guide to connect this Storyblok space with our project.
After installing the following packages, add this script to your package.json
file in the site
folder.
The above script will proxy our dev
site to run on https
this is required to load Storyblok data.
Furthermore, we have to add the Storyblok access token to the env.local
file.
Now you can open two terminals and run two commands npm run dev
and npm run proxy
. Now if you go to https://localhost:3010/
you will see the site is working as if nothing changed.
Now let's add the following code in _app.tsx
file in your pages folder.
If we breakdown the above code we can see we have
Grid
component and this component take two optional propslayout
andvariant
and within this, we have threeProductCard
components.- Next, we have a
Marquee
component and this component takes an optional propvariant
and within this, similar to the aboveGrid
it also has threeProductCard
components. - Lastly, we have a
Hero
component that just takes two propsheadline
anddescription
.
Based on this now let's design our components in Storyblok.
Create components in Storyblok
To create new components let’s go to the Block library section in the Storyblok dashboard.
By default, four blocks come with an empty Storyblok project. We will delete all except the page
block and then create new blocks to match our eCommerce homepage layout.
Create our first component "HeroSection"
This will represent the existing Hero
component of our project. Based on this we will have two Text
field headline
and description
. You can match the below screenshot.
Second component "ProductGrid"
Next, we are going to create a ProductGrid
component. This will represent the Grid
component and looped ProductCard
within this. Let's create a new Nestable Block like the one above and name it ProductGrid
.
After the block is created we are going to add a new field to this Block named variant
and this will be a Single-Option field
Now we can edit this field and add two options. This will let us select between these two values.
We can learn about all the props our Grid
component takes in the following path site/components/ui/Grid
.
Seeing that we can add the above two values as options in our variant
Single-Option Field.
Next, we will repeat the above step and create a new field named layout
with the following options. layout?: 'A' | 'B' | 'C' | 'D' | 'normal'
These options and field names are not limited to what we are writing. This is just to match our current code base. You are free to add new fields and more options as you like.
The last field for this component will be products
and for this, we are going to select a new field called Plugin.
Now we need to edit this field and configure it as seen in the following screenshot:
You can find the endpoint
and access_token
in your BigCommerce dashboard. If you are not sure you can follow this guide on how you can generate the token.
Final component "MarqueeSlider"
This one will be pretty similar to the above ProductGrid
component. It will have two fields
variant
this will be a Single-Option Field with the following optionvariant: 'primary' | 'secondary'
products
this will be a Plugin Field, with the same option as the aboveProductGrid
Add the following components to our Home story
Lastly, we must add our newly created components to our Home story. For this tutorial, we are going to match the components to match our code base but with Stoyblok we are free to add new components if needed.
In the above component, you can see that we are looping through the products. Instead of passing the ProductCard
we are passing a component called SingleProduct
and this component takes only productID
.
The reason for doing this is following
- Incoming product details are very limited, and there is no way to get more info for each product from Storyblok (this is by design).
- The data coming from Storyblok is static/cached in Storyblok CDN but for an eCommerce site always updated information is more desirable.
This is why we are going to only take the productID
from Storyblok and then we will validate the data on our end and also get all the required info for ProductCard
component.
First, create an API endpoint that gets the product info by ID
This will give us the formatted product information that will match what we need to pass as props in ProductCard
component. Next, we can create our SingleProduct
component as follows.
Lastly, we have one component left MarqueeSlider
. let’s create it.
Now, we have created all the components that we defined in Storyblok let's pass these into _app.tsx
file.
Next, go to our index.tsx
page and replace this file with the following code.
We can already see how minimal our homepage code looks compared to before. Now let's visit https://localhost:3010/
and make sure our site works as expected.
We now don't have hard-coded products on our home page. We can easily change the products for each section and reorder components directly from the Storyblok Visual Editor and see the live preview. Once we publish it will reflect automatically on our site without touching the code.
Finally, by implementing Storyblok with our existing codebase we not only reduced the number of codes in our project but also added a lot of new possibilities. Now we can create new components and make all the parts of our site fully dynamic. We can reuse these components throughout our site to reduce development time and give the content creators much more flexibility.
Name | Link |
---|---|
Next.js Commerce Quick Start | https://developer.bigcommerce.com/api-docs/storefronts/nextjs-commerce |
Next.js Commerce | https://nextjs.org/commerce |
Bigcommerce API Accounts & OAuth Scopes | https://developer.bigcommerce.com/api-docs/getting-started/api-accounts |
Storyblok eCommerce Integrations | https://www.storyblok.com/docs/guide/integrations/ecommerce |