How to Build an Ecommerce Static Site with Jekyll, Contentful, and Commerce Layer
Tags: ecommerce, jekyll, webdev, showdev
Published: 2018-11-14 13:48:32

---
title: How to Build an Ecommerce Static Site with Jekyll, Contentful, and Commerce Layer
published: true
description: Static sites are a good alternative for enterprise-level ecommerce 
tags: ecommerce, jekyll, webdev, showdev
canonical_url: https://www.contentful.com/blog/2018/11/07/how-build-static-site-ecommerce-jekyll-contentful-commerce-layer/
---

![Build an Ecommerce Static Site with Jekyll, Contentful, and Commerce Layer](https://images.ctfassets.net/fo9twyrwpveg/2djFApVtJu0Ew04oeui0io/d33c6f485351320ba208ad1488caed70/20181106_Commerce_layer-01.png)

There are plenty of guides out there showing how to use static site generators (SSGs) for basic ecommerce stores — with most of them using [Snipcart](https://www.contentful.com/blog/2016/02/10/snipcart-middleman-contentful/?utm_campaign=static-site-jekyll-contentful-commerce-layer&utm_medium=referral&utm_source=devto&utm_content=static-site-jekyll-contentful-commerce-layer&utm_term=) to add shopping cart functionalities to a simple product catalog. 

The question is: are static sites a good alternative for enterprise-level ecommerce as well? 

We definitely think so, and this tutorial aims to prove just that.

## About this tutorial

By reading this guide, you will learn how to build a static site ecommerce with enterprise-level features. The resulting [demo site](https://contentful-commerce.netlify.com/) will be a multi-country, multi-language online shop with the following characteristics:

- Each country will have **its own catalogue** with custom product selection and sorting
- Each country will have **its own price list** with localized currency
- Each country will have **its own inventory** shipping from multiple warehouses

Instead of diving into the technical details, we will focus on the site architecture and development workflow. After learning the big picture, we recommend you explore the source code and follow the step-by-step guide that is available on [GitHub](https://github.com/commercelayer/contentful-commerce).

## Tools

To build our demo site, we selected some of the best tools, one for each piece of the site architecture:

* [Jekyll](https://jekyllrb.com/) as the SSG
* [Contentful](https://www.contentful.com/?utm_campaign=static-site-jekyll-contentful-commerce-layer&utm_medium=referral&utm_source=devto&utm_content=static-site-jekyll-contentful-commerce-layer&utm_term=) to manage content
* [Commerce Layer](https://commercelayer.io/) as the e-commerce platform
* [GitHub](https://github.com/) for code versioning 
* [Netlify](https://www.netlify.com/) for deployment and content delivery

It's worth mentioning that we are going to manage *all* content with Contentful, including the product information, categories, and catalogues. Unlike traditional platforms, Commerce Layer is a purely transactional engine that leaves content management to the CMS. This approach lets creatives and content editors design any customer experience, without locking them into any site structure or template system.

## Architecture and workflow

The picture below outlines how the tools fit together in the overall architecture. As you can see, each tool is dedicated to a specific step of the pipeline, contributing to the content editing and publishing workflow.

{: img }
![How the tools fit together in the overall architecture](//images.ctfassets.net/fo9twyrwpveg/7wyKYa2crCCCUemOY6sMgY/c0f1164469d6846c880ae45e99af32a6/image_0.png)

Let's have a look at each step of the diagram:

1. Merchants work on the product offering within Commerce Layer. SKUs - the items that are being sold - are imported into Contentful for content enrichment. Prices and stock information are not imported.
2. Content editors get the list of available SKUs and create products within Contentful. Their job is also to assign products to categories, categories to catalogues, and catalogues to countries. Published content is built into the Jekyll site on Netlify.
3. Developers work on the site structure, layout, and client-side functions. The code is built into a static site and deployed to Netlify. Each product variant is tagged with the related SKU code, using the *data-sku-code* data attribute on any DOM element. This tagging lets the client-side code interact with the Commerce Layer API in real-time. Every time the code changes, developers push the code to Github for versioning and continuous deployment. 
4. The client-side code fetches prices and inventory for each available SKU. It also adds shopping cart functionalities to the page and sends orders to Commerce Layer. Merchants fulfill orders through the OMS. Newly-created SKUs are sent to Contentful for content enrichment, feeding the same workflow.

## Content model

The second step of the workflow is where content editors manage the product catalogue in Contentful. This is a key step that determines how the site works. The diagram below outlines the content model that we designed to support the multi-country, multi-language structure:

{: img }
![Content model that we designed to support the multi-country, multi-language structure](//images.ctfassets.net/fo9twyrwpveg/1kmova7aFEcY0ciqccKi2Y/83875f4d6b9f0c779203ec106d830c12/image_1.png)

Let's describe each model, their roles and relationships:

* Countries have one associated catalogue, and the same catalogue can be associated to many countries. The market ID attribute determines the price list, inventory model, payment methods, shipping methods and promotions that are available for that country, as defined within Commerce Layer.
* Catalogues are sorted list of categories. This means that each country can have its own categories with custom sorting.
* Categories can have one or more lists of products by country. Each list has its own sorting, which means the same category can have a custom product merchandising for each country. If content editors don't define any specific list for a given country, the default one will be used.
* Products can have one or more variants. All content that is common to all their variants can be defined at the product level.
* Variants are the items that are being sold. The SKU code attribute links them to the SKUs defined in Commerce Layer and makes the variants shoppable. In our use case, variants can have a size.

## Continuous delivery

Our static site ecommerce is made of three types of data that need to be refreshed as they change: code, content, and commerce.

**When code changes**, developers push the changes to Github, triggering a new build on Netlify. The build command fetches all published content from Contentful (`jekyll contentful`) and builds the static site (`jekyll build`).

{: img }
![Build command fetches published content from Contentful and builds the static site](//images.ctfassets.net/fo9twyrwpveg/2CyRtcsFjekaMYYEMcsKkC/9e2c599dd9f1f63cd852834e1aa11402/image_2.png)

**When content changes**, content editors hit the publish button on Contentful. To trigger a new build for each publishing, we need to create a build hook on Netlify... 

{: img }
![Creating a build hook on Netlify](//images.ctfassets.net/fo9twyrwpveg/6AiWsiI0ogUKGeIG8QQsEo/bde3563a6c0b2a21763eb1a34bbf4c54/image_3.png)

...and link it to the publish webhook on Contentful:

{: img }
![Link it to the publish webhook on Contentful](//images.ctfassets.net/fo9twyrwpveg/4Ro8Wdbbzy0kaKu6SW4Yoc/ad2d0db739cf47b4846b45b308948b75/image_4.png)

**When commerce changes**,  we don't need to trigger any new build, since prices and inventory are dynamically fetched from the static site JavaScript.

## Understanding the mix of dynamic and static data

At this point what we have is a static site that includes product information based on the content stored in Contentful. To make this site a real shop we bring in the Commerce Layer JavaScript library which enriches the HTML with commerce data. 

When customers enter the site and choose a shipping country, this included JavaScript library gets an OAuth2 access token putting the related market ID in the request scope. **All the subsequent requests are scoped to that market**.

{: img }
![Customer chooses a country](//images.ctfassets.net/fo9twyrwpveg/2KzMdAHauAQkaKYay0mWiO/6ff50dfa12cd168ca67206c5e863bba8/image_5.png)

The market in scope determines the right price list, inventory model, promotions, payment and shipping methods as defined by the merchant within Commerce Layer. As a developer, you just need to use the right access token to get country-specific commerce data and let customers place orders without errors.

Being a static site, all pages are plain HTML files served by Netlify CDN. The only dynamic information is provided by Commerce Layer API as displayed by the graphic below (red boxes) for each relevant page.

{: img }
![Dynamic information provided by Commerce Layer API](//images.ctfassets.net/fo9twyrwpveg/4qgp7K3aiscmUUaKa8u4ae/ea8763b37c1ee7cbdadf62b09de5df30/image_6.png)

As you can see, not all pages require the same kind of data from Commerce Layer:

1. For each product, the category page fetches the price of the first variant
2. The product page fetches the selected variant price and availability message
3. The shopping bag is dynamically built from the current order line items
4. The shopping bag preview is always populated and the "Proceed to checkout" button is linked to the current order checkout URL, that is part of the order API response.

Considering that prices generally don't change very often - unless you have some adaptive pricing mechanism in place - we could also import prices into Jekyll as YAML files and leverage Commerce Layer webhooks system to trigger a new deploy on each price change. 

This would further improve the website speed by reducing the number of client-side requests to the Commerce Layer API. Pages, like the category overview page, could be almost completely static (apart from the shopping bag preview) as we wouldn't need to fetch the prices dynamically.

## Placing orders

When customers proceed to checkout, they are redirected to the Commerce Layer hosted checkout application, where they can add all the required information and place the order.

{: img }
![Commerce Layer hosted checkout page](//images.ctfassets.net/fo9twyrwpveg/47fnE4U4esAcu8qkcu6skq/94984bd99b855bd0adc68cc78fb8e3a1/image_7.png)

This is the easiest solution to reduce the time-to-market and start selling as soon as the content catalog is ready. That said, Commerce Layer also provides APIs to create a custom tailored experience on your own site. Just keep in mind that this would require us to add some server-side components to our static site, at least to manage the authentication and safely store its `client_secret`.

## Sum up

Static sites are a great alternative to database-driven websites. They are fast, scalable, secure and cost-effective. All amazing features that can make your ecommerce shine.

In this article, we described how some of the best tools could be combined to build the perfect stack - a.k.a. [JAMstack](https://jamstack.org/) - for static ecommerce. The separation of concerns between code, content, and commerce is pretty clear. The work of developers, content editors and merchants is perfectly orchestrated.

From a features perspective, we just scratched the surface of what we can do. There are a lot of great services that would perfectly fit in our diagram, making our static site even more *dynamic*. 

### Next steps – Improving functionality with more services 

Other than the fully branded checkout experience, our static site ecommerce could be improved adding the following features:

1. Full-text search and filtering, using a tool like [Algolia](https://www.algolia.com/)
2. Customer account, with access to their order history, address books and wallets (credit cards on file) through the Commerce Layer customer APIs.
3. Last but not least, better content. Despite focusing our demo on publishing a product catalogue, we strongly believe that commerce should be driven by content. A platform like Commerce Layer provides the security, performance and flexibility you need to sell your products with confidence. But this isn’t enough. Content must be creative, emotive, and inspiring.  By using Contentful, we can enrich our content model and build a unique customer journey. With our transactional engine sorted, we can focus on elevating our content experience to a whole new level.