Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netlify block #18758

Merged
merged 28 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading