serve robots.txt and sitemap.xml

Two exact paths a crawler asks for by name, so they are mux entries rather than
resolver cases — no bundle can collide, since a key always sits under a section.

robots.txt at the site root is served verbatim, because a site that ships one has
said something deliberate; otherwise the engine emits the minimum that is true and
points at the sitemap. The sitemap lists every bundle in every language it exists
in, since each variant is separately reachable, with lastmod only where a bundle
has a date. Every URL comes from content.URL like every other path the engine
emits, so a sitemap cannot disagree with what is actually served.

Both need a declared base. Without one the sitemap answers 404 rather than listing
paths no crawler can resolve, and robots omits the Sitemap line rather than
writing a relative one.

write() was setting text/html for every caller, and headers only go out with the
first byte — so a handler setting its own type would have had it silently replaced,
which is how a sitemap gets served as a web page. It now splits into write and
writeAs, and the tests assert the content types rather than only the bodies.
This commit is contained in:
2026-08-01 02:23:36 +06:00
parent 470e7f18b9
commit bfe980e028
16 changed files with 476 additions and 51 deletions
+45
View File
@@ -0,0 +1,45 @@
package content
import (
"testing"
"testing/fstest"
)
func TestSettingsAreOptionalAndNormalised(t *testing.T) {
// A bare site root must still serve, so an absent file is not an error (ADR-0026).
got, err := LoadSettings(fstest.MapFS{})
if err != nil || got.Base != "" || got.Title != "" {
t.Fatalf("absent site.yaml gave %+v, %v — want the zero value and no error", got, err)
}
got, err = LoadSettings(fstest.MapFS{
SettingsFile: {Data: []byte("base: https://khosra.example/\ntitle: খসড়া\n")},
})
if err != nil {
t.Fatal(err)
}
if got.Base != "https://khosra.example" {
t.Errorf("base = %q — a trailing slash must be trimmed, or every URL doubles it", got.Base)
}
if got.Title != "খসড়া" {
t.Errorf("title = %q", got.Title)
}
}
func TestMalformedSettingsAreAnError(t *testing.T) {
// Unlike one bad bundle, which is skipped loudly (ADR-0029), this misconfigures every page, so the
// caller treats it as fatal.
if _, err := LoadSettings(fstest.MapFS{SettingsFile: {Data: []byte("base: [unclosed\n")}}); err == nil {
t.Error("a malformed site.yaml must be an error")
}
}
func TestAbsoluteOnlyPrefixesWhenABaseExists(t *testing.T) {
for _, c := range []struct{ base, path, want string }{
{"https://khosra.example", "/posts/hello/", "https://khosra.example/posts/hello/"},
{"", "/posts/hello/", "/posts/hello/"},
} {
if got := Absolute(c.base, c.path); got != c.want {
t.Errorf("Absolute(%q, %q) = %q, want %q", c.base, c.path, got, c.want)
}
}
}