Where Does Logic Go on Jamstack Sites?
Publikováno: 24.8.2020
Here’s something I had to get my head wrapped around when I started building Jamstack sites. There are these different stages your site goes through where you can put logic.
Let’s look at a special example so you can see what I mean. Say you’re making a website for a music venue. The most important part of the site is a list of events, some in the past and some upcoming. You want to make sure to label them as … Read article “Where Does Logic Go on Jamstack Sites?”
The post Where Does Logic Go on Jamstack Sites? appeared first on CSS-Tricks.
You can support CSS-Tricks by being an MVP Supporter.
Here’s something I had to get my head wrapped around when I started building Jamstack sites. There are these different stages your site goes through where you can put logic.
Let’s look at a special example so you can see what I mean. Say you’re making a website for a music venue. The most important part of the site is a list of events, some in the past and some upcoming. You want to make sure to label them as such or design that to be very clear. That is date-based logic. How do you do that? Where does that logic live?
There are at least four places to consider when it comes to Jamstack.
Option 1: Write it into the HTML ourselves
Literally sit down and write an HTML file that represents all of the events. We’d look at the date of the event, decide in whether it’s in the past or the future, and write different content for either case. Commit and deploy that file.
<h1>Upcoming Event: Bill's Banjo Night</h1>
<h1>Past Event: 70s Classics with Jill</h1>
This would totally work! But the downside is that weu’d have to update that HTML file all the time — once Bill’s Banjo Night is over, we have to open your code editor, change “Upcoming” to “Past” and re-upload the file.
Option 2: Write structured data and do logic at build time
Instead of writing all the HTML by hand, we create a Markdown file to represent each event. Important information like the date and title is in there as structured data. That’s just one option. The point is we have access to this data directly. It could be a headless CMS or something like that as well.
Then we set up a static site generator, like Eleventy, that reads all the Markdown files (or pulls the information down from your CMS) and builds them into HTML files. The neat thing is thatwe can run any logic we want during the build process. Do fancy math, hit APIs, run a spell-check… the sky is the limit.
For our music venue site, we might represent events as Markdown files like this:
---
title: Bill's Banjo Night
date: 2020-09-02
---
The event description goes here!
Then, we run a little bit of logic during the build process by writing a template like this:
{% if event.date > now %}
<h1>Upcoming Event: {{event.title}}</h1>
{% else %}
<h1>Past Event: {{event.title}}</h1>
{% endif %}
Now, each time the build process runs, it looks at the date of the event, decides if it’s in the past or the future and produces different HTML based on that information. No more changing HTML by hand!
The problem with this approach is that the date comparison only happens one time, during the build process. The now
variable in the example above is going to refer to the date and time the build happens to run. And once we’ve uploaded the HTML files that build produced, those won’t change until we run the build again. This means that once an event at our music venue is over, we’d have to re-run the build to make sure the website reflects that.
Now, we could automate the rebuild so it happens once a day, or heck, even once an hour. That’s literally what the CSS-Tricks conferences site does via Zapier.
But this could rack up build minutes if you’re using a service like Netlify, and there might still be edge cases where someone gets an outdated version of the site.
Option 3: Do logic at the edge
Edge workers are a way of running code at the CDN level whenever a request comes in. They’re not widely available at the time of this writing but, once they are, we could write our date comparison like this:
// THIS DOES NOT WORK
import eventsList from "./eventsList.json"
function onRequest(request) {
const now = new Date();
eventList.forEach(event => {
if (event.date > now) {
event.upcoming = true;
}
})
const props = {
events: events,
}
request.respondWith(200, render(props), {})
}
The render()
function would take our processed list of events and turn it into HTML, perhaps by injecting it into a pre-rendered template. The big promise of edge workers is that they’re extremely fast, so we could run this logic server-side while still enjoying the performance benefits of a CDN.
And because the edge worker runs every time someone requests the website, we can be sure that they’re going to get an up-to-date version of it.
Option 4: Do logic at run time
Finally, we could pass our structured data to the front end directly, for example, in the form of data attributes. Then we write JavaScript that’s going to do whatever logic we need on the user’s device and manipulates the DOM on the fly.
For our music venue site, we might write a template like this:
<h1 data-date="{{event.date}}">{{event.title}}</h1>
Then, we do our date comparison in JavaScript after the page is loaded:
function processEvents(){
const now = new Date()
events.forEach(event => {
const eventDate = new Date(event.getAttribute('data-date'))
if (eventDate > now){
event.classList.add('upcoming')
} else {
event.classList.add('past')
}
})
}
The now
variable reflects the time on the user’s device, so we can be pretty sure the list of events will be up-to-date. Because we’re running this code on the user’s device, we could even get fancy and do things like adjust the way the date is displayed based on the user’s language or timezone.
And unlike the previous points in the lifecycle, run time lasts as long as the user has our website open. So, if we wanted to, we could run processEvents()
every few seconds and our list would stay perfectly up-to-date without having to refresh the page. This would probably be unnecessary for our music venue’s website, but if we wanted to display the events on a billboard outside the building, it might just come in handy.
Where will you put the logic?
Although one of the core concepts of Jamstack is that we do as much work as we can at build time and serve static HTML, we still get to decide where to put logic.
Where will you put it?
It really depends on what you’re trying to do. Parts of your site that hardly ever change are totally fine to complete at edit time. When you find yourself changing a piece of information again and again, it’s probably a good time to move that into a CMS and pull it in at build time. Features that are time-sensitive (like the event examples we used here), or that rely on information about the user, probably need to happen further down the lifecycle at the edge or even at runtime.
The post Where Does Logic Go on Jamstack Sites? appeared first on CSS-Tricks.
You can support CSS-Tricks by being an MVP Supporter.