Snipcart Integration with Webpack Powered Spike

May 25, 2017

It is time once again to show you how to easily integrate our shopping cart platform Snipcart into your projects. In this tutorial we'll play with the awesome static website generator that is Spike. It has been developed by the guys behind a lot of really nice web projects at Carrot Creative, so it's worth taking a look!

Let's get started with the ecommerce integration. If flexibility is what you’re after, then you’ll find yourself very pleased with a Spike + Snipcart combination for your online store.

For this tutorial, all you need is a Snipcart account (free in test mode). That’s it, that’s all!

How’s that? It’s because Spike is based on a simple ideology: "Out of the box, Spike will just take standard html, css, and js files and copy them over without any modification. It is up to you to build your ideal system out of plugins that transform your content."

In other words, it's a thin layer that leverages different "ecosystems" such as webpack, to bundle and generate a static website. You’re the one in charge of defining the build process. Luckily enough, Spike makes the whole process feel quite natural. This way, you can easily start with a beginner build without having to worry about losing any extensibility that might be required if you were to complexify your integration.

Now let’s get started. First, you’ll have to install Spike with the following command: npm install -g spike.

Next, you can use Spike's default template for our project using the following scaffolding command: spike new spike-snipcart.

If you plan on building and deploying your website on a VM, you’re also going to have to add Spike as a dependency with npm install --save spike.

1. Declaring the products

Before we can move forward, we’re going to create our products. For this demo, we’ll keep it simple and won't be using any external data. That being said, we still don’t want to input it directly into our views. To achieve this, we’ll create a data folder within our assets folder. From there, we’ll declare a new folder called products with the index.js file containing the following:

2. Tweaking our app.js

Now that we have the data, we’re going to have to inject it into our pages so that we can actually use it. To do this, we’ll have to go to our app.js file and use the following declaration to load it:
const products = require('./assets/data/products/index.js')

Then, we’re going to use the following reshape declaration to inject our products into the locals variable, just like this:

3. Creating our views

Now that we’ve got access to products in our views, let's use it! To keep this demo simple, we’ll wipe out the default index.sgr page and create a new one, which we’ll use to display our products instead.

You see, each product is iterated and a buy button is created for each and every one of them. Now, we’re only missing the necessary scripts for Snipcart, which we’ll add right into our layout. Note, that if you were to have multiple pages, we recommend you use an include to do so.

4. Booting up our website

Let's see how it looks now! Using the Spike CLI, use the following method: spike watch in your project folder. It’ll create a neat dev server that will serve your public build folder and won’t reload all code changes. Now, if you hit the website, this is what you should see.

5. Deploying

Now that that’s all done, we’ll use Netlify to deploy our site. You’ll see it’s pretty straightforward. Simply push your project to a Git repo and then hit up Netlify's website. From there, use spike compile as your build command, and public as your publishing directory. That’s all! Netlify will take charge of the rest! Take a look at our example. This is what it should look like:

There you have it! Your website should be working as it was locally!

It took me an hour or so to put this demo together. Although Spike has a good doc repertoire, I still had to do some extra digging to find some good examples for this demo. Fortunately, there’s an Open Source website that was created using Spike, which offers a wide variety of solid examples.

At the end of the day, I feel as though Spike is really powerful, which is awesome! But, it does take some getting used to if you really want to make the most of it. For example, I could have fetched some external data for the product build process, but I held back for the purpose of this demo. It’s something that I do plan on exploring in the future, though. It should be interesting. I’ll definitely keep you guys posted once I’ve given it a go! Feel free to fork the code on Github.