robotstxtv1
import robotstxtv1 "pocketone.dev/ext/contracts/robotstxt/v1"
Package robotstxtv1 is the robotstxt/v1 contract: the app's /robots.txt, the file crawlers read before anything else (RFC 9309). Extensions and the app's own pb_hooks add rules with Add, AddFunc and Private; the provider builds the file from them and serves it when the app has no robots.txt of its own.
Private paths are the contract's other half: a private prefix gets a Disallow line, is left out of the app's sitemap and llms.txt, and every response under it carries X-Robots-Tag: noindex. Other extensions read them with PrivatePrefixes, with or without a provider.
Source: ext/contracts/robotstxt/v1.
Constants
const Path = "/robots.txt"
Path is where the file is served.
Variables
var Contract = extkit.NewContract[Builder]("robotstxt/v1", extkit.One)
Contract is robotstxt/v1.
Functions
Add
func Add(app core.App, r Rule)
Add adds a rule to app's robots.txt. Call it any time (in Register, from pb_hooks), whoever provides the contract.
AddFunc
func AddFunc(app core.App, fn func(app core.App) []Rule)
AddFunc adds rules that change at run time (settings, a reloaded config). fn runs whenever the rules are read, which includes every request's private-path check, so it must be cheap: cache what it reads.
CleanPrefix
func CleanPrefix(p string) string
CleanPrefix is a path prefix as robots.txt writes it: trimmed, with a leading slash; "" when there's nothing.
Indexable
func Indexable(app core.App) bool
Indexable reports whether search engines may index the app: the provider's answer, or true without one.
IsPrivate
func IsPrivate(app core.App, urlPath string) bool
IsPrivate reports whether urlPath is under one of the app's private prefixes.
Private
func Private(app core.App, prefix, source string)
Private adds a private prefix: a Disallow line, left out of sitemaps and llms.txt, and X-Robots-Tag: noindex on every response under it. Anyone can read robots.txt, so a private prefix is public knowledge: it keeps pages out of search, it doesn't hide or protect them.
PrivatePrefixes
func PrivatePrefixes(app core.App) []string
PrivatePrefixes are the app's private path prefixes, sorted, without duplicates.
Under
func Under(urlPath string, prefixes []string) bool
Under reports whether urlPath is under one of prefixes: equal to it, or below it ("/account" covers "/account", "/account/" and "/account/x", not "/accounts").
Types
Builder
type Builder interface {
// Build returns the whole file for the request (absolute Sitemap: lines use its origin).
Build(e *core.RequestEvent) (string, error)
// Indexable reports whether search engines may index the app at all. When false, every response carries
// X-Robots-Tag: noindex and the sitemap is empty (dev mode, a staging app).
Indexable() bool
}
Builder builds the app's robots.txt.
Rule
type Rule struct {
// UserAgent is the group the line goes in; "" is "*".
UserAgent string
// Allow and Disallow are path prefixes; set one.
Allow string
Disallow string
// Private makes Disallow a private prefix (User-agent: * only): also left out of sitemaps and llms.txt, and
// served with X-Robots-Tag: noindex.
Private bool
// Sitemap is an extra Sitemap: line (absolute URL, or a path on this app).
Sitemap string
// Source names who added it (an extension, "pb_hooks", "settings"), for logs.
Source string
}
Rule is one contribution: a line under its user agent's group, or a Sitemap: line.
Rules
func Rules(app core.App) []Rule
Rules are every contribution, in the order added (for providers), the functions' after the fixed ones.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.