Skip to main content
import * as BunnySDK from "https://esm.sh/@bunny.net/[email protected]";

/\*\*

- When a response is not served from the cache, you can use this event handler
- to modify the response going from the origin.
- This modify the response before being cached.
-
- Returns an HTTP response.
- @param {Context} context - The context of the middleware.
- @param {Request} request - The current request done to the origin.
- @param {Response} response - The HTTP response or string.
\*/
async function onOriginResponse(context: { request: Request, response: Response }): Promise<Response> | Response | void {
let body = await context.response.text();
body = body.replace('</title>', '- bunny.net</title>');

    // We need to remove the original Content headers to make sure the response is returned correctly
    const headers = new Headers(context.response.headers);
    headers.delete('Content-Length');
    headers.delete('Content-Encoding');

    return new Response(body, {
      status: context.response.status, // Preserve the original status code
      headers,  // Preserve the original headers
    });

}

BunnySDK.net.http.servePullZone()
.onOriginResponse(onOriginResponse);