-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (48 loc) · 1.99 KB
/
index.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import repoLink from "../../components/repo-link.js";
export default {
title: "Includes at The Edge",
metaDescription: "Dynamically include content into templates at The Edge.",
page: function() {
return `
<section>
<h1>Include content into templates at The Edge</h1>
<p>
The ability to transform the content of an HTTP response with Edge Functions enables you to substitute content into templates as you would with Edge Includes.
</p>
<p>
In this example, we look for an <code>{{INCLUDE_PRICE_INFO}}</code> placeholder in our response, and replace it with some other content.
</p>
<pre><code>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);
};
</code></pre>
<p>This include Edge Function is set up to run using the <code>include=pricing</code> query parameter on any URL, using this entry in the netlify.toml file:</p>
<pre><code>[[edge_functions]]
function = "include"
path = "/*"</code></pre>
<h2>See this in action</h2>
<ul>
<li><a href="/some-content-page">View the original resource, before any replacement.</a></li>
<li><a href="/some-content-page?include=pricing">View the returned response after the include has been inserted.</a></li>
<li>${repoLink("include.ts")}</li>
</p>
</section>
`;
},
};