18 releases

0.4.2 Jul 9, 2024
0.4.0 Apr 5, 2023
0.4.0-pre4 Mar 8, 2023
0.3.1 Jun 20, 2022
0.1.3 Mar 12, 2021

#45 in Template engine

Download history 14/week @ 2024-03-31 2/week @ 2024-04-07 37/week @ 2024-05-05 9/week @ 2024-05-19 30/week @ 2024-06-23 1/week @ 2024-06-30 288/week @ 2024-07-07 18/week @ 2024-07-14

337 downloads per month
Used in esi_fastly

MIT license

451 lines

ESI for Fastly

This crate provides a streaming Edge Side Includes parser and executor designed for Fastly Compute.

The implementation is a subset of the ESI Language Specification 1.0 supporting the following tags:

  • <esi:include> (+ alt, onerror="continue")
  • <esi:comment>
  • <esi:remove>

Other tags will be ignored and served to the client as-is.

Example Usage

use fastly::{http::StatusCode, mime, Error, Request, Response};

fn main() {
    if let Err(err) = handle_request(Request::from_client()) {
        println!("returning error response");


fn handle_request(req: Request) -> Result<(), Error> {
    // Fetch ESI document from backend.
    let mut beresp = req.clone_without_body().send("origin_0")?;

    // If the response is HTML, we can parse it for ESI tags.
    if beresp
        .map(|c| c.subtype() == mime::HTML)
        let processor = esi::Processor::new(
            // The original client request.
            // Use the default ESI configuration.

            // The ESI source document. Note that the body will be consumed.
            &mut beresp,
            // Optionally provide a template for the client response.
            // Provide logic for sending fragment requests, otherwise the hostname
            // of the request URL will be used as the backend name.
            Some(&|req| {
                println!("Sending request {} {}", req.get_method(), req.get_path());
            // Optionally provide a method to process fragment responses before they
            // are streamed to the client.
            Some(&|req, resp| {
                    "Received response for {} {}",
    } else {
        // Otherwise, we can just return the response.


See example applications in the examples subdirectory or read the hosted documentation at docs.rs/esi. Due to the fact that this processor streams fragments to the client as soon as they are available, it is not possible to return a relevant status code for later errors once we have started streaming the response to the client. For this reason, it is recommended that you refer to the esi_example_advanced_error_handling application, which allows you to handle errors gracefully by maintaining ownership of the output stream.


The source and documentation for this project are released under the MIT License.


~153K SLoC