Skip to content

Instantly share code, notes, and snippets.

@SlashNephy
Created June 24, 2024 04:05
Show Gist options
  • Save SlashNephy/2a3032940ed9d71844b96d52f567c0d2 to your computer and use it in GitHub Desktop.
Save SlashNephy/2a3032940ed9d71844b96d52f567c0d2 to your computer and use it in GitHub Desktop.
すべてのクローラーをブロックする robots.txt を配信する Cloudflare Worker
const CONTENT = `
User-agent: *
Disallow: /
`.trim()
export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname.endsWith('/robots.txt')) {
return new Response(CONTENT, {
headers: {
'Content-Type': 'text/plain',
}
})
}
const response = await fetch(request);
const newHeaders = new Headers(response.headers);
newHeaders.set('X-Robots-Tag', 'none');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
})
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment