diff --git a/src/current/netlify.toml b/src/current/netlify.toml index cd8c1c38b45..7b8042f98be 100644 --- a/src/current/netlify.toml +++ b/src/current/netlify.toml @@ -2,12 +2,17 @@ base = "src/current/" publish = "_site/" command = "./netlify/build.sh" + edge_functions = "./netlify/edge-functions" [build.environment] NODE_VERSION = "18.14.0" RUBY_VERSION = "3.2.1" [build.processing.html] pretty_urls = true +[[edge_functions]] + path = "/*" + function = "blockBytedance" + #[[plugins]] # package = "@netlify/plugin-lighthouse" # [plugins.inputs] diff --git a/src/current/netlify/edge-functions/blockBytedance.ts b/src/current/netlify/edge-functions/blockBytedance.ts new file mode 100644 index 00000000000..48798656749 --- /dev/null +++ b/src/current/netlify/edge-functions/blockBytedance.ts @@ -0,0 +1,32 @@ +import type { Config, Context } from "@netlify/edge-functions"; + +export default async function handler(request: Request, context: Context) { + const bytedanceUserAgents = [ + 'Bytespider; spider-feedback@bytedance.com' + // Add other Bytedance user agents if needed + ]; + + // Get the user agent from the request headers + const userAgent = request.headers.get('user-agent') || ''; + + // Check if the user agent matches any of the Bytedance user agents + const isBytedanceBot = bytedanceUserAgents.some(ua => userAgent.includes(ua)); + + // Block access if it is a Bytedance user agent + if (isBytedanceBot) { + return new Response('Access Denied', { + status: 403, + headers: { 'Content-Type': 'text/plain' } + }); + } + + // Log "hello" before continuing the request + console.log("hello"); + + // Continue the request chain and get the response from the next handler or the origin server + return context.next(); +} + +export const config: Config = { + path: "/*", // This path can be adjusted based on where you want to apply this edge function +}; diff --git a/src/current/package.json b/src/current/package.json index c5f19f72dcc..00cd1d13d42 100644 --- a/src/current/package.json +++ b/src/current/package.json @@ -7,6 +7,7 @@ "jest-cli": "^26" }, "dependencies": { + "@netlify/edge-functions": "^2.10.0", "front-matter": "^4.0.2", "redoc-cli": "^0.11.4", "snippet-enricher-cli": "^0.0.8",