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 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
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

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

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou&#039;s conic-gradient() polyfill was the last item:

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

See all articles