Home Web Front-end JS Tutorial A modular Edge Side Includes component for JavaScript Compute

A modular Edge Side Includes component for JavaScript Compute

Dec 20, 2024 am 03:13 AM

A modular Edge Side Includes component for JavaScript Compute

In the summer of 2022, my teammate Kailan worked on a Rust crate for Fastly Compute, implementing a subset of the Edge Side Includes (ESI) templating language, and published a blog post about it. This article was significant not just because we released a useful library, but because it was a brilliant illustration of what Compute can bring us: a programmable edge with modular functionality. And now that JavaScript has been generally available on Compute for more than a year, it was time we had a similar solution for our JavaScript users. Fastly’s ESI library for JavaScript, now available on npm, allows you to add powerful ESI processing to your application.


Programmability and Modularity

For almost a decade, Fastly’s CDN has had support for Edge Side Includes (ESI), a templating language that works by interpreting special tags in your HTML source. It revolves around the tag , which instructs the edge server to fetch another document and inline it into the stream.

index.html

1

2

3

4

5

6

<body>

<esi:include src="./header.html" />

<main>

Content

</main>

</body>

Copy after login
Copy after login
Copy after login
Copy after login

header.html

1

<header>Welcome to the web site</header>

Copy after login
Copy after login
Copy after login
Copy after login

output

1

2

3

4

<header>Welcome to the web site</header>

<main>

Content

</main>

Copy after login
Copy after login
Copy after login

When Compute entered the scene, the edge landscape changed in two main ways: programmability and modularity.

Soon after our platform support for Rust had stabilized, we published a crate for Rust that implemented ESI, and added programmability. You could now configure, using code, how to build additional backend requests, and how to handle response fragments. You could even perform ESI processing on documents that don’t originate from the backend server. This programmability differentiated it from the ESI support we have in VCL, which was limited to the fixed set of features we offered.

At the same time, this approach was highly modular, giving the programmer a choice to perform this ESI processing on a per-request basis, and the option to combine the processing with other modules that work with compatible data types, and apply them in any order and/or logical condition specified.

Next target: JavaScript

Similar to our Rust release, we wanted this JavaScript library to be programmable. Fastly’s JavaScript support has always embraced the Fetch API and its companion Streams API. One useful feature of the Streams API is the TransformStream interface, allowing for data to be “piped” through an object in order to apply a transformation—perfect for ESI. By implementing the ESI processor as an implementation of TransformStream, we were able to fit it right into a Fastly Compute Application written in JavaScript.

Here's how you can stream through it:

1

2

3

4

5

6

<body>

<esi:include src="./header.html" />

<main>

Content

</main>

</body>

Copy after login
Copy after login
Copy after login
Copy after login

The transformation, which we call the EsiTransformStream, is applied to the stream directly, alleviating memory and performance concerns. This means:

  • There is no need to buffer the entire upstream response before applying the transformation.
  • The transformer consumes the upstream response as quickly as it can, and processes ESI tags as they show up in the stream. As the transformer finishes processing each ESI tag, the results are released immediately downstream, so we are able to hold the minimal possible buffer. This allows the client to receive the first byte of the streamed result as it becomes ready, without having to wait for it to be processed in entirety.

In addition, this design is modular, making the EsiTransformStream just another tool you can use alongside other things. For example, you might have other transformations you want to apply to responses, like compression, and a response can be piped through any number of these transform streams, since it's a completely standard interface. If you wanted to, you could even conditionally enable ESI for just certain requests or responses, such as by request headers, paths, or response content type.

Here's how you instantiate EsiTransformStream:

1

<header>Welcome to the web site</header>

Copy after login
Copy after login
Copy after login
Copy after login

The constructor takes a URL and a Headers object, and optionally takes some options as a third parameter. As described earlier, the main functionality of ESI is to download additional templates, for inclusion into the resulting stream. Encountering an tag uses fetch as the underlying mechanism to retrieve a template, and the main purpose of these parameters is to configure those fetch calls:

  • The URL is used to resolve relative paths in the src of tags.
  • The Headers are used when making the additional requests to fetch the templates.
  • The optional configuration object can be used to override the behavior of the fetch that is made, and to apply other custom behavior, such as how to process the fetched template, and custom error handling.

In the simplest case, you’ll use just the fetch value of the configuration object. If you don’t provide it, then it uses the global fetch function instead, but in Compute you’ll need it to specify a backend for the fetch to use when including a template (unless you’re using the dynamic backends feature). The example snippet above assigns the backend named origin_0 before calling the global fetch.

That’s it! With this simple setup you can have a backend serving ESI tags and a Compute application processing them. Here’s a full example that you can run:

fiddle.fastly.dev

Support for ESI features

This implementation offers more ESI features than others we have made available in the past.

Error handling

Sometimes, a file referenced by an tag may fail to fetch due to it not existing or causing a server error. It’s possible to ignore the error in these cases by adding the attribute onerror="continue".

1

2

3

4

5

6

<body>

<esi:include src="./header.html" />

<main>

Content

</main>

</body>

Copy after login
Copy after login
Copy after login
Copy after login

If /templates/header.html causes an error, the ESI processor silently ignores the error and replaces the entire tag with an empty string.

It’s also possible to use more structured error handling by employing an block, which looks like this:

1

<header>Welcome to the web site</header>

Copy after login
Copy after login
Copy after login
Copy after login

The ESI processor first executes the contents of . If an esi:include tag causes an error, then the ESI processor executes the contents of .

It’s important to note that the entire block is replaced by the entirety of the block if it succeeds or the if there is an error. In the above example, if /templates/header.html causes an error, then this also causes the text "Main header" not to appear in the output; only the text "Alternative header" is included. See the ESI language specification for more details.

Conditionals

ESI also allows conditional execution, by performing runtime checks on variables. The following is an example of such a check:

1

2

3

4

<header>Welcome to the web site</header>

<main>

Content

</main>

Copy after login
Copy after login
Copy after login

When the processor encounters an block, it runs through the blocks, checking the expressions set in their test attributes. The processor executes the first esi:when block where the expression evaluates to true. If none of the expressions evaluates to true, then it will optionally execute the esi:otherwise block if it’s provided. The entire block is replaced by the entirety of whichever or block that executes.

The processor makes available a limited set of variables, which are based primarily on request cookies. In the above example, an HTTP cookie by the name “group” is checked for its value. Our implementation is based on the ESI language specification; refer to it for more details.

List of supported tags and features

This implementation supports the following tags of the ESI language specification.

  • esi:include
  • esi:comment
  • esi:remove
  • esi:try / esi:attempt / esi:except
  • esi:choose / esi:when / esi:otherwise
  • esi:vars

The tag is defined in the spec to be optional, and is not included in this implementation.

ESI Variables are supported in the attributes of ESI tags, and ESI Expressions are supported in the test attribute of . Additionally, the comment is supported.

Custom behavior means endless possibilities

While the feature set is enough to get thrilled about, the truly exciting part of being programmable is that even more things are possible. Bringing in templates is the main use of ESI, but that’s by no means all that it can do. Here’s an example.

Consider you have a timestamp marked up in your document that you want represented as a relative date when it’s displayed, such as “2 days ago”. There are many ways of doing this, but to have the best performance and memory implications, it would be great to perform a find/replace in streams. Programming this ESI library can actually be used as a good option for doing this.

We can define timestamps to be encoded in your backend document using a specially-crafted ESI tag in a format such as the following:

1

2

3

4

5

6

<body>

<esi:include src="./header.html" />

<main>

Content

</main>

</body>

Copy after login
Copy after login
Copy after login
Copy after login

For example, this snippet can represent midnight on January 1, 2024, Pacific time:

1

<header>Welcome to the web site</header>

Copy after login
Copy after login
Copy after login
Copy after login

Now, set up the EsiTransformStream to serve a synthetic replacement document whenever it sees that URL pattern:

1

2

3

4

<header>Welcome to the web site</header>

<main>

Content

</main>

Copy after login
Copy after login
Copy after login

Now, when the processor encounters the example snippet above, it will emit a result similar to the following (depending on how many days into the future you run it):

1

2

3

4

5

6

7

8

9

const transformedBody = resp.body.pipeThrough(esiTransformStream);

 

return new Response(

  transformedBody,

  {

    status: resp.status,

    headers: resp.headers,

  },

);

Copy after login

Because the backend document is cacheable by Fastly, future requests can benefit from a cache HIT, while the processing will continue to display the updated relative time.

For a live example of this, view the following fiddle:

fiddle.fastly.dev

Take it for a spin!

@fastly/esi is now available on npm, ready to be added to any JavaScript program. Use it at the edge in your Fastly Compute programs, of course, but in fact, it even works outside of Compute, so long as your environment supports the fetch API. The full source code is available on GitHub.

Get started by cloning either of the Fiddles above and testing them out with your own origins, even before you have created a Fastly account. When you’re ready to publish your service on our global network, you can sign up for a free trial of Compute and then get started right away with the ESI library on npm.

With Compute, the edge is programmable and modular – choose and combine the solutions that work best for you, or even build your own. We aren’t the only ones who can provide modules like this for Compute. Anyone can contribute to the ecosystem and take from it. And, as always, meet us at the Fastly community forum and let us know what you’ve been building!

The above is the detailed content of A modular Edge Side Includes component for JavaScript Compute. 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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles