Documentation
¶
Overview ¶
Package robots writes and serves an app's robots.txt from a Policy. By default, AI crawlers and agents may read none of the site, and every other crawler may read all of it. An app adds the paths it keeps from search engines, and marks a non-production deploy as NoIndex.
policy := robots.Policy{Disallow: []string{"/auth/", "/api/"}, NoIndex: os.Getenv("NOINDEX") != ""}
mux.Handle("GET /robots.txt", policy)
handler := policy.Wrap(mux)
robots.txt asks crawlers to stay out. A crawler can ignore it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AIAgents = []string{
"GPTBot", "ChatGPT-User", "OAI-SearchBot",
"ClaudeBot", "Claude-User", "Claude-SearchBot", "anthropic-ai",
"Google-Extended", "Applebot-Extended",
"PerplexityBot", "Perplexity-User",
"CCBot", "Bytespider", "Amazonbot",
"Meta-ExternalAgent", "Meta-ExternalFetcher", "FacebookBot",
"cohere-ai", "cohere-training-data-crawler",
"MistralAI-User", "DuckAssistBot", "YouBot", "AI2Bot",
"Diffbot", "ImagesiftBot", "Omgilibot", "Timpibot",
}
AIAgents lists the user agents of AI crawlers and agents: training crawlers, AI search indexers, and fetchers that act for a user. A Policy blocks them unless AllowAI is set. Link-preview fetchers (facebookexternalhit, Slackbot, Twitterbot, Discordbot) aren't on it, so a shared link still previews. Google-Extended and Applebot-Extended opt a site out of AI use and leave Google and Apple search alone. New agents appear often, so the list changes between releases.
Functions ¶
This section is empty.
Types ¶
type Policy ¶
type Policy struct {
// AllowAI lets AIAgents read the site under the same rules as every other crawler.
AllowAI bool
// Blocked names more user agents that may read nothing. They join AIAgents rather than
// replacing them.
Blocked []string
// Disallow lists path prefixes that the other crawlers may not read, like "/api/".
Disallow []string
// Sitemap is the sitemap's absolute URL, written as a "Sitemap:" line when set.
Sitemap string
// NoIndex marks a deploy that no crawler should read, like a staging version. Text then
// disallows everything for everyone, and Wrap adds "X-Robots-Tag: noindex, nofollow".
NoIndex bool
}
Policy says which crawlers may read which paths. The zero value blocks AIAgents and lets every other crawler read the whole site.
func (Policy) ServeHTTP ¶
func (p Policy) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP writes p.Text() as text/plain. Mount it at "GET /robots.txt", ahead of any handler that takes every path.
func (Policy) Text ¶
Text returns the robots.txt for p. A crawler follows only the group that names it most specifically, so the blocked agents get a group of their own, and "User-agent: *" covers the rest. A group with nothing disallowed carries an empty "Disallow:", which allows everything.