forked from netlify/edge-functions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry-block.ts
34 lines (29 loc) · 950 Bytes
/
country-block.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
26
27
28
29
30
31
32
33
34
import { Context } from "https://edge.netlify.com";
export default async (request: Request, context: Context) => {
// Here's what's available on context.geo
// context: {
// geo: {
// city?: string;
// country?: {
// code?: string;
// name?: string;
// },
// subdivision?: {
// code?: string;
// name?: string;
// },
// }
// }
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" },
});
};