robots

package
v0.7.49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func (p Policy) Text() string

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.

func (Policy) Wrap

func (p Policy) Wrap(h http.Handler) http.Handler

Wrap returns h unchanged unless p.NoIndex is set. With NoIndex, every response carries "X-Robots-Tag: noindex, nofollow". A search engine that reaches a page by a link, without reading robots.txt first, still leaves it out of its results.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL