-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 1.45 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
import repoLink from "../../components/repo-link.js";
export default {
title: "Block content according to country",
metaDescription: "Use geolocation data to block content according to country.",
page: function () {
return `
<section>
<h1>Block content according to country</h1>
<p>You can use geolocation data to identify a user's country and block content if required.</p>
<p>Geolocation information is available on the <code>Context.geo</code> object.</p>
<pre><code>import { Context } from "https://edge.netlify.com";
export default async (request: Request, context: Context) => {
const BLOCKED_COUNTRY_CODE = "GB";
const countryCode = context.geo?.country?.code || "US";
const countryName = context.geo?.country?.name || "United States of America";
if (countryCode === BLOCKED_COUNTRY_CODE) {
return new Response(`We're sorry, you can't access our content from ${countryName}!`, {
headers: { "content-type": "text/html" },
status: 451,
});
}
return new Response(`Hello there! You can freely access our content from ${countryName}!`, {
headers: { "content-type": "text/html" },
});
};</code></pre>
<h2>See this in action</h2>
<ul>
<li>Check if you're in a blocked country by running the <a href="/country-block">country block Edge Function</a></li>
<li>${repoLink("country-block.ts")}</li>
</ul>
</section>
`;
},
};