forked from netlify/edge-functions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude.ts
25 lines (19 loc) · 781 Bytes
/
include.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Context } from "https://edge.netlify.com";
export default async (request: Request, context: Context) => {
// Just return what was requested without transforming it,
// unless we fnd the query parameter for this demo
const url = new URL(request.url);
if (url.searchParams.get("include") !== "pricing") {
return;
}
console.log("Including pricing content into the page");
// Get the page content
const response = await context.next();
const page = await response.text();
// Search for the placeholder
const regex = /{{INCLUDE_PRICE_INFO}}/i;
// Replace the content
const pricingContent = "It's expensive, but buy it anyway.";
const updatedPage = page.replace(regex, pricingContent);
return new Response(updatedPage, response);
};