Skip to content

Commit

Permalink
Merge pull request #18758 from cockroachdb/feat/netlify-block
Browse files Browse the repository at this point in the history
netlify block
  • Loading branch information
ebembi-crdb authored Jul 30, 2024
2 parents 143a8bf + e50451e commit 75c2d98
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/current/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
32 changes: 32 additions & 0 deletions src/current/netlify/edge-functions/blockBytedance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Config, Context } from "@netlify/edge-functions";

export default async function handler(request: Request, context: Context) {
const bytedanceUserAgents = [
'Bytespider; [email protected]'
// 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
};
1 change: 1 addition & 0 deletions src/current/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 75c2d98

Please sign in to comment.