Skip to content

Commit 98e3bdd

Browse files
authored
Merge pull request netlify#26 from netlify/context-site-example
add example for context.site
2 parents 34f275b + ee20c84 commit 98e3bdd

File tree

9 files changed

+74
-3
lines changed

9 files changed

+74
-3
lines changed

Diff for: netlify.toml

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
path = "/error"
8282
function = "error"
8383

84+
[[edge_functions]]
85+
path = "/context-site"
86+
function = "context-site"
87+
8488
# Serve pages explaining each example
8589
[[edge_functions]]
8690
path = "/example/*"

Diff for: netlify/edge-functions/[page].js

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import pageLocalizedContent from "../../pages/localized-content/index.js";
2525
import pageProxyRequests from "../../pages/proxy-requests/index.js";
2626
import pageEnvironment from "../../pages/environment/index.js";
2727
import pageUncaughtExceptions from "../../pages/uncaught-exceptions/index.js";
28+
import pageContextSite from "../../pages/context-site/index.js";
2829

2930
// The keys here correspond to the path in the request to `/example/PATH`
3031
const pages = {
@@ -48,6 +49,7 @@ const pages = {
4849
"proxy-requests": pageProxyRequests,
4950
environment: pageEnvironment,
5051
"uncaught-exceptions": pageUncaughtExceptions,
52+
"context-site": pageContextSite,
5153
};
5254

5355
export default async (Request, Context) => {

Diff for: netlify/edge-functions/context-site.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Context } from "netlify:edge";
2+
3+
export default async (request: Request, context: Context) => {
4+
return new Response(`Hello from ${context.site.name}!`);
5+
};

Diff for: pages/context-site/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
![Netlify examples](https://user-images.githubusercontent.com/5865/159468750-df1c2783-39b2-40da-9c0f-971f72a7ea3f.png)
2+
3+
# Access Site Information from Edge Functions
4+
5+
Netlify Edge Functions give access to site information via `context.site`.
6+
7+
## Code example
8+
9+
Edge Functions are files held in the `netlify/edge-functions` directory.
10+
11+
```ts
12+
import type { Context } from "netlify:edge";
13+
14+
export default async (request: Request, context: Context) => {
15+
return new Response(`Hello from ${context.site.name}!`);
16+
};
17+
```
18+
19+
- [Explore the code for this Edge Function](../../netlify/edge-functions/context-site.ts)
20+
21+
## View this example on the web
22+
23+
- https://edge-functions-examples.netlify.app/example/context-site
24+
25+
## Deploy to Netlify
26+
27+
You can deploy this and all the other examples in this repo as a site of your own to explore and experiment with, by
28+
clicking this button.
29+
30+
[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/netlify/edge-functions-examples&utm_campaign=devex&utm_source=edge-functions-examples&utm_medium=web&utm_content=Deploy%20Edge%20Functions%20Examples%20to%20Netlify)

Diff for: pages/context-site/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import repoLink from "../../components/repo-link.js";
2+
3+
export default {
4+
title: "Access Site Information from Edge Functions",
5+
metaDescription: "Access Site Information from an Edge Function",
6+
page: function () {
7+
return `
8+
<section>
9+
<h1>Access Site Information from Edge Functions</h1>
10+
<p>Netlify Edge Functions give access to site information via <code>context.site</code>.</p>
11+
<pre><code>import type { Context } from "netlify:edge";
12+
13+
export default async (request: Request, context: Context) => {
14+
return new Response(\`Hello from \${context.site.name}!\`);
15+
};</code></pre>
16+
17+
<h2>See this in action</h2>
18+
<ul>
19+
<li><a href="/context-site">See response from code snippet</a></li>
20+
<li>${repoLink("context-site.ts")}</li>
21+
</ul>
22+
<h2>Related</h2>
23+
<ul>
24+
<li><a href="/example/environment">Environment variables</a></li>
25+
</ul>
26+
</section>
27+
`;
28+
},
29+
};

Diff for: pages/cookies-delete/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default async (request: Request, context: Context) => {
3030

3131
## View this example on the web
3232

33-
- https://edge-functions-examples.netlify.app/example/cookies
33+
- https://edge-functions-examples.netlify.app/example/cookies-delete
3434

3535
## Deploy to Netlify
3636

Diff for: pages/cookies-read/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default async (request: Request, context: Context) => {
3030

3131
## View this example on the web
3232

33-
- https://edge-functions-examples.netlify.app/example/cookies
33+
- https://edge-functions-examples.netlify.app/example/cookies-read
3434

3535
## Deploy to Netlify
3636

Diff for: pages/cookies-set/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default async (request: Request, context: Context) => {
3030

3131
## View this example on the web
3232

33-
- https://edge-functions-examples.netlify.app/example/cookies
33+
- https://edge-functions-examples.netlify.app/example/cookies-set
3434

3535
## Deploy to Netlify
3636

Diff for: pages/home/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default {
6868
<li class="home__sectionListItem"><a class="home__sectionListItemLink" href="/example/log">Write to the logs</a></li>
6969
<li class="home__sectionListItem"><a class="home__sectionListItemLink" href="/example/environment">Use environment variables</a></li>
7070
<li class="home__sectionListItem"><a class="home__sectionListItemLink" href="/example/uncaught-exceptions">Read logs for uncaught exceptions</a></li>
71+
<li class="home__sectionListItem"><a class="home__sectionListItemLink" href="/example/context-site">Access site information</a></li>
7172
</ul>
7273
</section>
7374
`;

0 commit comments

Comments
 (0)