Getting Started with Jamstack
Tags: jamstack, nextjs, javascript, html
Published: 2021-07-22 14:17:50

> Over the past couple of years, one tech stack that has consistently stood out from the crowd in developer conversations is Jamstack. In this article, we're going to go over what makes Jamstack so unique, as well as how you can get started creating your own Jamstack web app.

---

Let's get right into it!

##What is Jamstack?
Jamstack is an architecture designed to make your web app faster, more secure, and easier to scale. Many people believe Jamstack is the next evolution in modern web development - and here's why.

Jamstack is arguably the lightest technical stack out there. It eliminates the need to worry about infrastructure and servers, as it requires no real interaction with a database and relies mostly on third-party APIs in order to provide additional functionality, such as authentication and payment.
A Jamstack app consists of static HTML pages, made dynamic using JavaScript and enhanced with third-party services via API and prebuilt Markup. That's where its name comes from **J**avaScript, **A**PI, and hypertext **M**arkup.

##How Does Jamstack Work?
Though JAMstack confusingly includes the word "stack", it is more of architecture for web applications that promise to be faster, cheaper, and more secure than the traditional model. The traditional model is the client-server model, where applications are served dynamically from a web server.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qh2titgxr0ihb1nrl6s.png)

The key concept behind Jamstack is to eliminate the need for a backend server for the app, at least in the form with which you may be accustomed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjf20w96cfcw4qugiyma.png)

Instead, Jamstack sites are simply static files that are served by a special type of file-hosting called a CDN (Content Delivery Network). A CDN is actually a network of file servers around the world, all holding a copy of your site's files.
Once your static site has been loaded by a browser, you can use JavaScript to generate HTTP requests to third-party services, including databases, search, identity management, payment processing, and serverless functions.

##Jamstack Benefits
So what are the benefits of Jamstack? Why go through the hassle of transforming your web app architecture?
Jamstack's architecture is faster, cheaper, and more secure, which is made possible by the usage of a CDN and the elimination of a web server.

**1. A CDN is generally faster than a webserver!**
Since a CDN does not process requests and instead just serves files, the request/response loop is much shorter. And since CDNs can serve on the network edge, closer to the user, the latency of the response is minimized.

**2. A CDN is most likely less expensive than a webserver!**
If you use a web server, it must run 24/7 listening for requests. Since cloud computing is charged by the minute, you'll be paying for your server, even when it's idle.
CDN hosting is generally less expensive because a CDN service is only charged by the volume of data served, and therefore it's idle time doesn't cost anything.

**3. A CDN is generally more secure than a webserver!**
Finally, unlike a web server, a CDN does not process requests and therefore has limited potentially exposed attack vectors for hackers. A Jamstack site will still connect to services, such as cloud databases, but since these are managed by specialist third parties, security gaps are usually minimal.

---

##How Do I Get Started using Jamstack?
###Building the Frontend
Let's start with the Jamstack frontend. While the core of a Jamstack website is the static HTML that's delivered to the browser, JavaScript and the UI frameworks that build that experience are what have made its architecture popular.
A common front-end framework for Jamstack is Next.js. The following demonstrates how to get started with Next.js for Jamstack development. The easiest way to get started is by typing the following command in your terminal, and I'll name the project mynextapp:

`$ npx create next-app mynextapp`

which will generate for us a default boilerplate page.
Navigate into the newly made directory we just created by opening the folder in your code editor or by typing in the terminal:

`$ cd mynextapp`
`$ code .`

The following is an example of the default starter Next.js file tree, generated by the create-next-app command:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyssryqssnlmgyqwv350.png)

* The `node_modules` folder is where all the dependencies are stored.
* The pages folder is where we keep all of our page components. The idea is that each page in Next.js has its own React component. At the moment, the only component we have is the `index.js` component, which will be for the homepage.
* The api folder is for API endpoints.
* The `app.js` file acts as the root component, and all of our different page components are rendered here:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efqruj8zv1i3t1ri1gpb.png)

* The public folder is where all of our public assets are stored, such as images.
* The styles folder contains all of our CSS files and CSS modules.
* `.gitignore` is for version control.
* `package.json` is to keep track of our various dependencies.

In the `package.json` file, we have the `dev` script, which spins up a local development server for us, to enable us to preview our app in the browser.

In the terminal, run the following command:

`$ npm run dev`

In your browser, type `localhost:3000`, and the following will be displayed:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kitop8xpxvwzt3vx9tsz.png)

The content shown in your browser is defined in the `index.js` component. Replace all the content inside the main <div> with a simple heading:

{% gist https://gist.github.com/LorielSphere/f4c3cc96fddb005aed668567a54213f4 %}

Now, we can see our new content displayed in the browser:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yluedk4i4t9xzr306n0.png)

---

The `index.js` component is being pre-rendered before it even reaches the browser. During development, this is done via server-side rendering. The server renders the component, then sends the HTML file to the browser, which happens on each page request from the browser.

##Pages and Routes
The pages folder is where we create all of our page components. Each page in Next.js is driven by a React component. So for an About page, we'd have an About component, for a Pricing page, we'd have a Pricing component, and so on. Each page component has its own file inside the pages folder. Therefore, the file name and location of each component are tied to the route for that particular page.

For example, in the pages folder, let's create an `about.js` file. Next.js will automatically create a route `/about`, which is the name of the file, to serve up this component. The route name is tied to the file name.

The one exception to the rule is `index.js`. Next.js doesn't create a route `/index`, it just creates the route /. So all we have to do is go to the root of the domain to see the `index.js` component.
In the `about.js` file, let's create a stateless functional component and export it:

{% gist https://gist.github.com/LorielSphere/495d366bcb41959b86532e5ece3947c4 %}

Go to `/about` in the browser to see the About component page:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uj7o1lb8kiwc11d2otyp.png)

We can also create subfolders in the pages folder that contain page components.

Let's create a subfolder called pricing and a file called `plan1.js`. In the `plan1.js` file, we'll create a stateless functional component called Plan1:

{% gist https://gist.github.com/LorielSphere/8533cccaa4753bfffb2e489539c8c027 %}

When we have a subfolder, Next.js will create a route according to the subfolder as well. Therefore, in this case, the route is: localhost:3000/pricing/plan1
And that's how easy it is to make a multi-page website with Next.js.

---

##Building the Backend
Content is at the heart of any website or application. This may take a variety of forms, such as a blog or an online store. Tools like Auth0 provide user authentication and management, which can present a number of challenges on its own.

There are additionally a number of cloud projects that make it simple to build serverless functions, from which you can begin developing your own backend service.

The great thing about Jamstack apps is that they serve as a central point for bringing in any feature or service you require, paving the way for powerful solutions that are focused on addressing your specific challenge.

---

##Deploying Your App
Deploying a website and managing CI/CD systems is arguably one of the most difficult aspects of developing a web application.
In the Jamstack world, tools such as Codesphere offer automated DevOps to make deployment easier.

---

##What Now?
Given the advantages of Jamstack, many people in technology are expecting Jamstack's popularity to increase. Jamstack is growing rapidly, and may one day become the most popular architecture on the web.

If you want to have a go at building your own Jamstack app or a solution to the painful CI/DC system management, go to [Codesphere](https://link.codesphere.com/BO) provides an online code editor in which you can build, test, and deploy your apps all within a single tab of your browser.

Thanks for reading!