How to Build a Storefront with Nuxt and BigCommerce
Storyblok is the first headless CMS that works for developers & marketers alike.
Have you wondered how to integrate Storyblok with an e-commerce solution like BigCommerce and build a Storefront using Nuxt SDK?
In this tutorial, we are going to build this store and go step by step from 0 to a showcase of your products in a modern setup.
You can see the final result here https://storyblok-nuxt-bigcommerce-starter.netlify.app/
if you are in a hurry, you can check the whole source code of the project here github.com/storyblok/big-commerce-nuxt-starter
Storyblok Space
Sign up for a free Storyblok account & choose Create a new space {1}. Then name your Storefront {2}, and click the create space {3} button.
Once you created a new space, follow our eCommerce Storefront documentation for setting up your storefront in Storyblok.
Storefront using Nuxt
We have prepared a convenient Nuxt starter template to show you how to connect the eCommerce plugin with the eCommerce API to build your storefront. For this specific example, we are going to use BigCommerce API as our eCommerce provider.
Start by cloning the starter repository.
Once you have the source code in your local, make sure to install the correspondent dependencies using your package manager of choice npm install
.
Connecting your Storyblok Space with the Storefront
To connect our recently created space with our Storyblok instance, jump to the Space Settings {1} and navigate to Access Token {2} to get your credentials.
To make sure that the visual editor functions correctly, run the server with https enabled.
Add a Location (default environment) {3} pointing to your https://localhost:3000/
which is the default Nuxt server.
Rename the .env-template
file into .env
and add both your Storyblok Token {2} and your BigCommerce credentials.
Then pass your Storyblok Token reference to your Nuxt module configuration
Nuxt provides a handy runtime config API to expose your configuration within your application and server routes. We'll place our BigCommerce credentials here:
If the API is CORS restricted, you might need to create a separate token for your localhost/production server. In the case of BigCommerce & Nuxt.js, you need to create a new token for https://localhost:3000 for the local development and then create a different token with the real URL once you deploy your store.
After you finish setting up your tokens, we are ready to start. Launch that development server:
Changing the Real Path Field
When you open your Home Page in Storyblok, you will see a page not found
error. Since Storyblok automatically creates a /home
path from the Home story, you want to redirect that to your base URL in the Nuxt application /
which uses pages/index.vue
component template.
To fix this routing issue, change the real path of your homepage. When you open the home entry, you should already see the running localhost on port 3000 {1}. Now navigate to the Entry Configuration tab {2} and change the Real path field to /
{3}.
Reload the page and you should see your Nuxt application in the preview space. Be sure you hit the Save button before reloading. You probably notice that there is only a header component with a Storyblok Store logo and the rest is blank. This is because we haven’t yet created the block schemas to pair with the available components in the starter project.
Setting up the Storyblok Components
For this demo, we are going to build a home page with the following components:
- A Hero component
- A Single Product section to show a featured product
- A Product Grid to showcase the latest products
Hero
This component consists of the following fields:
- A
headline
text field. {1} - A
subheadline
to add a text to invite the user to the store. {2} - A background
image
of the store. {3}
Once created and added to our Home
page, write down the headline and upload an image in the asset field, then click Save. The page should reload and the hero component should appear. Change the headline again to make sure the preview in the Visual Editor is working just fine. You should be seeing something like this:
Here is the code for the hero component in Nuxt.
Single Product
This component showcases a single product retrieved from the eCommerce integration with BigCommerce:
Let’s add a Headline
field of type Text {1}, a Textarea field for the Description
{2} and a Text or Color (Plugin) {3} field for the background of the product image (optional). To get the actual product we need to set up the eCommerce integration {4} with a couple of more steps:
Create a new tab called Integration and inside, add a field of type Plugin
. Now click on the recently created field to configure it:
On the Custom Type Dropdown {1} select the sb-bigcommerce
plugin. Next fill up the options {2} with your BigCommerce credentials:
endpoint
: Your storefront URLtoken
: Your storefront API tokenlimit
: How many items do you want the content editor to be able to select.selectonly
: set thisproduct
(this limits the selection to either products or categories)
Once you click Save & Back to Fields {3} try to add a product from your store clicking on Select Products, this will open a special modal window. If everything is set up correctly, you should be able to see all your products listed and be able to select one of them.
Et voilá, you managed to get your product showcased on a component right there on the page.
Here is the code for the Nuxt component counterpart:
Product Grid
Now that you know how to set up the eCommerce plugin field type, try to replicate the same on this Blok by yourself. Remember to change the limit
of products you can select in the options to be more than 1. You should end with something looking similar to this:
Here is the code for the component on your Nuxt starter:
You might notice that we are using a useStore
composable to fetch products based on the ids
coming from Storyblok API and then save them in a local state. Why? Let’s find out in the next section.
Fetching product data
If you take a closer look to the JSON we fetch from the Storyblok API, you will notice that the bigcommerce_products
field contains and array with bare-minimum necessary info about the product like the id
, sku
and the image
:
If you want to retrieve additional data that would be useful for the end user, such as the price
you would need to retrieve that data directly using the GraphQL Storefront API.
So let’s create a composable useStore
to save the products on a local state and handle the GraphQL queries:
Create a local state using Vue’s reactive
with an array property products
and some async
functions to fetchProducts
based on an array of ids
and another fetchProductById
in case you want to retrieve just one.
The next step is to provide the fetchProducts
function with the correct url
of the storefront and some http options
. Here, the runtime config variables you configured at the beginning of this tutorial will come handy:
Now, time to query some products. First create and options variable and extend the baseOptions + the body with the graphql query.
In Summary, the query should retrieve products based on the array of ids provided, paginated (3 per page), and with data about the price (value and currency) and some formatting for the product images. Then, the data can be requested and formatted to match what’s expected on the UI.
Awesome, now you have everything you need to get the product information on your components.
Conclusion
Connecting two headless-based systems like BigCommerce and Storyblok can greatly enhance your workflow and the user experience. With Storyblok's Visual Editor, you get a great editing experience, while still keeping all your store and product information in your eCommerce system.
The other big advantage is in terms of performance. With a Jamstack-based storefront, you can decrease page loading times and hopefully increase purchases. If you want to learn more about why Storyblok is a great choice as a CMS for your eCommerce experiences, we recommend reading our article CMS for eCommerce.