Home >Web Front-end >JS Tutorial >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.
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
index.html
<body> <esi:include src="./header.html" /> <main> Content </main> </body>
header.html
<header>Welcome to the web site</header>
output
<header>Welcome to the web site</header>Content
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.
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:
<body> <esi:include src="./header.html" /> <main> Content </main> </body>
The transformation, which we call the EsiTransformStream, is applied to the stream directly, alleviating memory and performance concerns. This means:
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:
<header>Welcome to the web site</header>
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
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:
This implementation offers more ESI features than others we have made available in the past.
Sometimes, a file referenced by an
<body> <esi:include src="./header.html" /> <main> Content </main> </body>
If /templates/header.html causes an error, the ESI processor silently ignores the error and replaces the entire
It’s also possible to use more structured error handling by employing an
<header>Welcome to the web site</header>
The ESI processor first executes the contents of
It’s important to note that the entire
ESI also allows conditional execution, by performing runtime checks on variables. The following is an example of such a check:
<header>Welcome to the web site</header>Content
When the processor encounters an
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.
This implementation supports the following tags of the ESI language specification.
The
ESI Variables are supported in the attributes of ESI tags, and ESI Expressions are supported in the test attribute of
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:
<body> <esi:include src="./header.html" /> <main> Content </main> </body>
For example, this snippet can represent midnight on January 1, 2024, Pacific time:
<header>Welcome to the web site</header>
Now, set up the EsiTransformStream to serve a synthetic replacement document whenever it sees that URL pattern:
<header>Welcome to the web site</header>Content
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):
const transformedBody = resp.body.pipeThrough(esiTransformStream); return new Response( transformedBody, { status: resp.status, headers: resp.headers, }, );
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:
@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!