Home Web Front-end CSS Tutorial Netlify Edge Handlers

Netlify Edge Handlers

Mar 28, 2025 am 10:07 AM

Netlify Edge Processor Early Access: Redefining Jamstack

Netlify edge processors are currently in the early access stage (you can request access), but their power is worth a deep dive now. I think it will change the nature and possibility of Jamstack.

You understand CDN (Content Distribution Network). They are global and can geographically place resources near users, thus speeding up the website. Netlify provides CDN services for all content. The more content you can deploy on a CDN, the better. Jamstack advocates placing resources and pre-rendered content on global CDNs, with speed being its main advantage.

Traditionally, Jamstack and CDN's mindset is this: We need to weigh the pros and cons. To get the speed advantage of CDN, we did more work when building, not when rendering. But in doing so, we also lose some of the dynamic ability to use the server. Or, we're still doing dynamic things, but since we have to do so, we can only do it when the client renders.

This mindset is changing. Edge processors show that you no longer need to make this trade-off. You can perform dynamic server-side operations and stay on the global CDN. Here is an example:

  1. Your website has a /blog section that you want to return the latest blog posts stored in some cloud database. This edge processor only needs to run in /blog , so you configure the edge processor to run only at this URL.
  2. You write a JavaScript file to get these articles and place them in /edge-handlers/getBlogPosts.js .
  3. Now, when you build and deploy, this code will only run at that URL and do its work.

So what type of JavaScript code are you writing? It's very focused. I think 95% of the cases you replace the original response directly. For example, the HTML code for /blog on your website might be:

<meta charset="UTF-8"><meta content="width=device-width, initial-scale=1.0" name="viewport"><title> Testing Netlify edge functions</title><div></div>
Copy after login

With an edge processor, it is not particularly difficult to get the original response, make cloud data calls, and replace content with a blog post.

 export function onRequest(event) {
  event.replaceResponse(async() => {
    // Get the original response HTML
    const originalRequest = await fetch(event.request);
    const originalBody = await originalRequest.text();

    // Get data const cloudRequest = await fetch(
      `https://css-tricks.com/wp-json/wp/v2/posts`
    );
    const data = await cloudRequest.json();

    // Replace empty div with new content
    // For greater robustness, you can use Cheerio or other similar tools const manipulatedResponse = originalBody.replace(
      `<div></div> `,
      `
        <h2>
          <a href="https://www.php.cn/link/441ba8b924a353d6ec1ac4bff30df801">${data[0].title.rendered}</a>
        </h2>
        ${data[0].excerpt.rendered}
      `
    );

    let response = new Response(manipulatedResponse, {
      headers: {
        "content-type": "text/html",
      },
      status: 200,
    });

    return response;
  });
}
Copy after login

(I use the REST API of the website as an example of cloud data storage.)

This is much like the client's fetch, except that it does not operate the DOM after requesting some data, but rather happens before the response first arrives in the browser. The code runs on the CDN itself ("Edge").

Well, this is certainly slower than pre-rendered CDN content, because it requires additional network requests before responding, right? There is indeed some overhead, but it's faster than you think. Network requests occur on the network itself, so using superfast computers on superfast networks. It may only take a few milliseconds. In any case, they only allow 50 milliseconds of execution time.

I've successfully run all of this on my approved account. What's great is that you can test them locally using the following command:

 netlify dev --trafficMesh
Copy after login

This works well in both development and deployment.

Anything you output in console.log() can be set in the Netlify dashboard.

Here is a repository link containing my normal functioning edge processor: [Repository link] (The actual repository link should be inserted here)

Netlify Edge Handlers

The above is the detailed content of Netlify Edge Handlers. 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week&#039;s roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I&#039;ve been aware of it for a while, but haven&#039;t taken it for a spin yet. It has some pretty cool and

Paperform Paperform Apr 16, 2025 am 11:24 AM

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Apr 17, 2025 am 11:26 AM

In this week&#039;s roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook&#039;s

Options for Hosting Your Own Non-JavaScript-Based Analytics Options for Hosting Your Own Non-JavaScript-Based Analytics Apr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

See all articles