Codementor Events

A Practical Introduction to Jamstack

Published Sep 30, 2019Last updated May 03, 2023
A Practical Introduction to Jamstack

What is JAM Stack?

If you’re into Web Development and have worked with one or two Javascript Frameworks at one point, you most likely have come across the terms MERN, MEAN, and, most recently, MEVN Stack. MongoDB serves as your non-relational database in these stacks, while ExpressJS and Nodejs are your web server. One similarity between these stacks is the presence of a web server. This is where the Jamstack differs.

JAM stands for Javascript API Markup, an abbreviation of the technologies used in building a web application. Here’s a pretty straight forward explanation of what JAM Stack is from the official Jamstack website:

A modern architecture for creating fast, secure sites and dynamic apps with JavaScript, APIs, and pre-rendered Markup, served without web servers.

Why is it necessary?

From the definition, “without web servers” is in bold text because this is the basic idea of JAM Stack. With JAM Stack, front-end developers can build useful, functional web applications without the need for a backend.

With just your frontend skill, you can build awesome useful applications thanks to the Jamstack specification.

Some examples of existing sites built with JAM Stack are:

To learn more about jamstack, check out the official jamstack website: https://jamstack.org/

A Practical Example

1*KN0HrIrT3yHiP8ZrVJjUDQ.png

We’ll be building Chucklarious. Chucklarious is a collection of random Chuck Norris jokes. If you’re not sure who Jon Skeet is, he’s infamously referred to as the Chuck Norris of Programming. I had mentioned a bit about him here:

Problem Solving Hacks - by Martins Onuoha

Prerequisites

  • Basic knowledge of HTML, CSS, and Javascript
  • Familiarity with Javascript’s fetch API.
  • We’ll be working with the opensource Internet Chuck Norris Database API

Folder Structure

We have a basic folder structure, you can ignore the Img folder.
1*WzzO4zN-k--HPAOOU-CFmg.png

Markup

From the screenshot of Chucklarious, you’d notice we have a basic structure of top Navbar, Cards and a FAB at the bottom right.

  • I’ll be using MaterializeCSS however, feel free to use whatever you want for styling.
  • For Content rendering and semantic templating, I’ll be using Handlebarsjs

1*ZnNktomPtoIer8Qn5mRthg.png

If you checked the live example you would notice a preloader just before the page loads. The Navbar comes right after the preloader.

1*oSUvH-zaxH3XNkX-WWRH4A.png

1*CJQia_zSf3-nxxqjZP7Z1A.png

Next, we’ll build out a content to show the user in case of failed page load.

1*VdBsICZfildc-cmQafro0w.png

For content rendering, we’re using Handlebars, Handlebarjs is a minimal templating engine. Handlebars templates look like regular HTML, with embedded handlebars expressions. Those expressions have a format like so {{ some expression }}. These expressions make it easy to render contents dynamically, one of which is the {{ each }} which works like a regular for loop in Javascript.

1*ADtF-pkh2m12Iy2o8fl8sA.png

Notice we have a button for reacting to jokes. I’ve attached an on-click event to the button, called the LMFAO (terrible choice of name by the way) function and passed the joke id as an argument.

1*CGJIjjqkQWWvsJ9QLVlXhg.png

We would also need a prompt just before the user closes the application.

1*sex1p2fAgKF1iAU3BjfdaQ.png

Finally, we include the scripts at the bottom, right before the closing body tag:

1*YMhULeQNLpWJUfdDrzxDWQ.png

Styles

Let’s add a bit of styling for the preloader and card content:

1*0uyUfv8zn7mdxCbkkTzR5w.png

API

I mentioned earlier that we would be using the icndb.comAPI. They provide an endpoint to fetch random jokes and also limit the number of jokes to fetch per request.
The endpoint would look like this:

https://api.icndb.com/jokes/random/10

Let’s see what the structure of our JSON response would look like:

1*tgy98GjLZeQNV8xwY5h6XA.png

First, we’d handle fading in and out of the preloader and toggling the FAB.

1*am4ItnV9tcCib4xWYH4bWg.png

Next, we call the getData and showTip functions once the page is done loading:
1*NScaK5rai2nHaPwGizcveQ.png

Let’s implement the getData function:

Using the Javascript fetch function, we’ll send a GET request to the endpoint, get the JSON response and set it as the value of the “jokes” key of the context object. We’ll also grab the content of the script element and compile it to a template so it can be executed. Finally, we render the compiled template:

1*Vr-oqfpRTwvVn_K--jgYzg.png

Next, we implement the ShowTip function:

1*EOPyeOl5Z4bezRN6_mO7rg.png

Finally, we write a function to reload the jokes and another to react to a joke:

1*9vC9ULdoRR5S3G3iVk31Gg.png

If you’ve come this far, then I’d expect you both understand the idea behind JAM Stack and have successfully replicated Chucklarious. 🎉🎉🎉

Be sure to read more on the JAM Stack ecosystem and get your hands dirty with practice.

You can find a live example here. The source code also lives here.

Cheers. 🍻

Discover and read more posts from Martins Victor Onuoha
get started
post commentsBe the first to share your opinion
Show more replies