paint-brush
A Tutorial to Bundle Cockpit CMS & Nuxt.js in a full JAMstackby@charlesouellet
376 reads
376 reads

A Tutorial to Bundle Cockpit CMS & Nuxt.js in a full JAMstack

by Charles OuelletNovember 27th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Three years ago, we fell down a rabbit hole.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - A Tutorial to Bundle Cockpit CMS & Nuxt.js in a full JAMstack
Charles Ouellet HackerNoon profile picture

Photo by Austin Neill on Unsplash

Three years ago, we fell down a rabbit hole.

We landed in a strange frontier of web development.

Talks of “static” and “serverless” sites, dedicated APIs, and burgeoning frontend wonders were common. JavaScript was everywhere. Developers were excited to build sites again. We had entered the JAMstack universe.

On our blog, we’ve already discussed countless headless CMS to store content and JS frameworks to render it.

Today, we keep exploring: 1) Cockpit CMS, an API-first CMS recommended by a friend, and 2) Nuxt.js, a kickass Vue.js framework (we’ve wanted to use it ever since we fell in love with Vue).

We’ll cover:

  • How to set up a Cockpit CMS installation & plug it with Snipcart.
  • How to use Nuxt.js static generation to render the frontend of our app.

The result? A full JAMstack app: headless content management, frontend rendered in a JS framework, & shopping through an e-commerce API. We’ll also throw in a live demo and code repo at the end!

But first, a few important definitions.

What is Cockpit CMS?

Our content data will be stored in Cockpit:

Cockpit is an open source, API-driven CMS that lets you implement your site without compromises.

Developer Artur Heinze crafted a system made to build simple dynamic websites without jumping through the hoops of WordPress, Drupal and the likes.

Its API-first approach (aka “headless”) allows for content management that is decoupled from content rendering. You can learn more about it in this primer on headless CMS.

Cockpit runs on PHP and is, at its core, a developer-first CMS.

Here are some of its cool features:

  • Self hosted
  • Fast & lightweight: runs on a lightweight database engine, no need for full SQL server
  • Flexible: implement on your site the way you want
  • Expandable: easy for devs to create their own modules
  • Modern & simple UI
  • Built-in tools: markdown, assets, thumbnails, etc.

What is Nuxt.js?

Nuxt.js will have our frontend covered for this one.

We had the chance to play with Vue.js a lot lately, and were naturally intrigued by the Vue application framework that is Nuxt.

In a nutshell, Nuxt allows you to create Universal Vue.js apps where your JavaScript can execute on both client and server.

Its main scope is UI rendering while abstracting away the client/server distribution. Like Vue, it can be used to start a project from scratch, or to improve on an existing one.

Inspired by React’s Next.js, Nuxt is a go-to solution for building SSR (Server Side Rendered) Vue apps. It’s also pretty good at generating static sites, thanks to its generate command.

And here’s a peek at how this little marvel actually works:

Nuxt.js rendering process [source]

We’ve seen comprehensive Nuxt.js examples & tutorials pop up (here & here) recently. In this one, we’ll see how an actual integration of Nuxt.js with an API-first CMS can work.

Cockpit CMS Tutorial with Nuxt.js & Snipcart

Pre-requisites

Before building our awesome cosplay shop demo (yup, that’s right), you’ll need:

If you are hosting Cockpit locally

  • PHP >= 5.4
  • PDO with SQLite support (or MongoDB)
  • GD extension enabled

If you are hosting it somewhere

1. Installing Cockpit CMS

Go to the CMS download page. We’ll use the “Official release” here, to avoid unstable features.

I’ll install Cockpit on its own instance. It’ll then be possible to access it with cockpit.local, and the public website with snipcart-shop.local.

Once the installation is complete, go to cockpit.local/install to finish up.

If you intend to push this installation live, you should change your username and password.

2. Creating products to sell with Snipcart

We’ll create products in our CMS first.

Start a new collection called Product. A collection is a way to hold all the content from a data structure. It's in a way the bread and butter of Cockpit CMS; lots of operations are done in this section.

In this collection, we add the following fields:

  • A field text named Name.
  • A field text named Description.
  • A field media named Image.
  • A field text named Price.

We now have to generate an API key to use Cockpit’s services, to do so, go to Settings (hamburger menu, top left corner), then General > API > Generate API token.

Finally, we only need to add our products to our shop’s content repo:

For this demo, we installed Cockpit on an Azure free site. It was very easy to set up: we pushed our code on GitHub and enabled automatic Git deploys in Azure.

3. Using & understanding Nuxt.js

For the client-facing store itself, we’ll dive into Nuxt.

First off, I must say I loved working with Nuxt. It’s super straightforward, their documentation is neat, and, as you already know, we’re currently on a honeymoon with Vue.js.

First thing to get started is to install Nuxt and scaffold our project. We’ll need vue-cli installed. If you don't have it, run: npm install vue-cli -g.

Then, we’ll scaffold the project with the Nuxt starter template:

That’s it for the installation. You can start the dev server using:

npm run dev

Nuxt.js key concepts

Let’s talk a bit about how Nuxt works. The first thing to understand is the pages. A page in Nuxt is kind of a route. It's a Vue component that will be automatically added to the application routes. For instance, in the default files, you have this component: pages/index.vue. This component will be rendered when you hit your application root URL at http://localhost:3000 (if you're running the dev server).

If you want to create a products page, you could add a component named products.vue in the pages folder and then you would get this URL working: [https://localhost:3000/products](https://localhost:3000/products.).

Nuxt takes care of the routing automatically; you don’t have to register routes or anything. The component will be mounted instantly, and you will even be able to render a statically generated site with it. We’ll do this later in the tutorial.

There’s also another important concept in Nuxt: components. You'll see this folder in the project created by the starter template. In it, you'll want to add child components to be used in your pages. We'll create one in the post.

Another thing we’ll see are the layouts. Layouts are used to define the skeleton of your site, it's usually where you will add your navigation and footer for example.

Last thing we’ll tackle is the nuxt.config.js file. It allows you to define which libraries or CSS you want to include globally and much more.

You can also add your own configuration settings if needed. This is what we’ll do first, with Cockpit settings. We’ll use these when calling Cockpit CMS API to retrieve products data.

Configuring Nuxt.js

Open nuxt.config.js and add:

We’ll replace <cockpit-url> with our own URL and <api-token> with the API token we've generated in Cockpit.

You’ll now have access to these environment variables in your pages.

Creating our layout

Before creating the first page, we’ll need to make some changes to the layout. In addition to pages and components, Nuxt also has a concept of layouts. Consider it a "master" page. This page must contain a special tag: <nuxt />.

Mine looks like:

Page components will be rendered inside the <nuxt /> tag.

Creating pages

Now for our first page. Open index.vue and remove everything. Let's start from scratch.

We’ll begin by creating the template part of the component. This template will be in charge of rendering all products.

Asking yourself where do the columns, is-mobile, is-multiline, is-centered, etc. classes come from? I used [Bulma](http://bulma.io/), a CSS framework for the demo. It provides useful components and styles to get a good looking demo quickly.

To add the reference to Bulma, I opened nuxt.config.js and added this object in the link array:

While we’re here, we’ll also add Snipcart’s default CSS:

Our head object should now look like:

Any stylesheets added there will be included automatically in all pages.

Notice the small-product tag? This is a component we'll create later.

Now, we need to fetch our products. Nuxt provides a very awesome method for pages component: asyncData. It's important to know that this method can't be used in child components, only in pages components.

This method will allow us to fetch data remotely before rendering the page itself. It’s especially useful when creating a static site!

To interact with Cockpit API, we’ll use Axios, a popular HTTP client in JavaScript:

npm install axios --save

Our component should look like:

In the asyncData method, I used Axios to fetch my products and generate thumbnails. Cockpit offers API endpoints for that. Notice the parameters the method receives: {env, params}. The env object will contain the settings set in nuxt.config.js.

Your page component will receive a list of products when mounted. Awesome, right?

Creating components

For this to work, we’ll need to create the component SmallProduct.vue. In the components folder, create a new file named SmallProduct.vue.

This component will render product details and Snipcart’s buy button.

Now run:

npm run dev

Open your browser to http://localhost:3000 and you should see something awesome!

4. Deploying our JAMstack bundle

Job’s (almost) done! Our Cockpit + Nuxt store lacks one last thing: deployment. As we often do, we’ll use Netlify for hosting. Just create a new site in Netlify and use:

npm install && npm run generate

We’ll set the publish directory to dist.

Give Netlify some time to build your site, and you’re done!

GitHub repo & live demo

Go ahead, check out the demo & code repo for this tuts!

See GitHub code repo

See live Cockpit + Nuxt.js demo

Conclusion

Well, this was a long one. I’ll keep the outro brief. ;)

While Cockpit doesn’t offer hosted instances, it provides developers with a very malleable content API. Especially if you’re a PHP fan. Based on my tests, perf looks quite good & stable too. It’s still a young, free headless CMS, and there’s room to improve, visually and functionally speaking. Good news is: it’s open source! So don’t hesitate to jump in and collaborate.

As for Nuxt, well, I loved it! I guess it transpired enough in the post. Please give it a try if you want to fast-track SSR or static Vue apps development.

If you’ve enjoyed this post, please take a second to share it on Twitter. Comments, questions? Section below is all yours!

I originally published this on the Snipcart Blog and shared it in our newsletter.

https://upscri.be/hackernoon/