Where Does Logic Go on Jamstack Sites?
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.
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>
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...
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 %}
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), {}) }
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>
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') } }) }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It'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.

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

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's like this.

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.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

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

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

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...
