Table of Contents
Coming soon: Bill's Banjo Night
Ended: Jill's 70s classic concert
Coming soon: {{event.title}}
Ended: {{event.title}}
{{event.title}}
Home Web Front-end CSS Tutorial Where Does Logic Go on Jamstack Sites?

Where Does Logic Go on Jamstack Sites?

Apr 02, 2025 pm 03:23 PM

The best choice for logical code under Jamstack architecture

A key issue when building a Jamstack website is determining the best placement of logical code. This article explores four different logical processing methods through an example of a music venue website and analyzes their respective advantages and disadvantages.

Where Does Logic Go on Jamstack Sites?

Assume that the core function of the website is to present a list of concerts, including ended and upcoming events, which need to be distinguished by date.

Solution 1: Write HTML directly

The most direct way is to write all activity information directly into the HTML file. This method is simple and easy to understand, but the disadvantage is that every time you update the activity information, you need to manually modify the HTML file and redeploy it, which is inefficient and error-prone.

<h1 id="Coming-soon-Bill-s-Banjo-Night"> Coming soon: Bill's Banjo Night</h1>
<h1 id="Ended-Jill-s-s-classic-concert">Ended: Jill's 70s classic concert</h1>
Copy after login

Solution 2: Process structured data during construction

A more efficient way is to use structured data (such as Markdown files or headless CMS) to store activity information and then use a static website generator (such as Eleventy) to process the logic during the build process. This method allows complex logical operations such as date comparisons, API calls, etc. to be performed at build time.

For example, use a Markdown file to represent activity:

 ---
title: Bill's Banjo Night date: 2020-09-02
---

Activity description...
Copy after login

Use templates to judge dates during construction:

 {% if event.date > now %}
<h1 id="Coming-soon-event-title">Coming soon: {{event.title}}</h1>
{% else %}
<h1 id="Ended-event-title">Ended: {{event.title}}</h1>
{% endif %}
Copy after login

However, the date comparison of this method is performed only once at build time. This means that after the event, the website needs to be rebuilt to update the information. While the build process can be automated, frequent builds can increase costs and there is still a risk of data lag.

Solution 3: Edge computing processing logic

As edge computing technology matures, code processing logic can be run at the CDN layer. This ensures that the latest data is fetched every request and provides extremely high performance. However, edge computing technology is not yet popular yet.

 // This code example is incomplete, for reference only 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), {})
}
Copy after login

Solution 4: Runtime processing logic

Finally, you can pass the structured data directly to the front-end, and then use JavaScript to perform logical operations on the user device to dynamically update the DOM. This method adjusts how dates are displayed based on the user's language and time zone and ensures that the information is always up to date.

For example, use data attributes to store date information:

<h1 id="event-title"> {{event.title}}</h1>
Copy after login

Then use JavaScript for date comparison:

 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')
  }
 })
}
Copy after login

Conclusion: Logical position selection depends on specific requirements

Which logical processing method to choose depends on the specific requirements. For static content, it is only possible to write HTML directly or build-time processing; for content that changes frequently, it is recommended to use CMS and build-time processing; for content that requires high real-time performance, edge computing or run-time processing needs to be considered. We need to weigh factors such as efficiency, cost and data real-timeness to choose the most appropriate solution.

The above is the detailed content of Where Does Logic Go on Jamstack Sites?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles