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:
+19
-2
@@ -15,11 +15,19 @@ import (
|
||||
// Handler serves a site.
|
||||
//
|
||||
// One mux entry, because URL shape is the resolver's business rather than the mux's: see resolve.
|
||||
func Handler(site *content.Site, r *render.Renderer, siteFS fs.FS) http.Handler {
|
||||
func Handler(site *content.Site, r *render.Renderer, siteFS fs.FS, settings content.Settings) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /", func(w http.ResponseWriter, req *http.Request) {
|
||||
serve(w, req, site, r)
|
||||
})
|
||||
// Two exact paths a crawler asks for by name, so they are mux entries rather than resolver cases: no
|
||||
// bundle can own them, since a key always sits under a section.
|
||||
mux.HandleFunc("GET "+robotsPath, func(w http.ResponseWriter, req *http.Request) {
|
||||
serveRobots(w, req, siteFS, settings.Base)
|
||||
})
|
||||
mux.HandleFunc("GET "+sitemapPath, func(w http.ResponseWriter, req *http.Request) {
|
||||
serveSitemap(w, req, site, settings.Base)
|
||||
})
|
||||
if siteFS != nil {
|
||||
if sub, err := fs.Sub(siteFS, "static"); err == nil {
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", serveStatic(sub)))
|
||||
@@ -98,7 +106,16 @@ func serveTags(w http.ResponseWriter, req *http.Request, site *content.Site, r *
|
||||
|
||||
// write sends a rendered page, logging a failed write rather than pretending it succeeded.
|
||||
func write(w http.ResponseWriter, out []byte, what string) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
writeAs(w, "text/html; charset=utf-8", out, what)
|
||||
}
|
||||
|
||||
// writeAs sends bytes with the type they actually are.
|
||||
//
|
||||
// Separate from write because headers are only sent with the first byte, so a handler that set its own type
|
||||
// before calling write would have had it silently replaced by HTML — which is how a sitemap ends up served
|
||||
// as a web page.
|
||||
func writeAs(w http.ResponseWriter, contentType string, out []byte, what string) {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
if _, err := w.Write(out); err != nil {
|
||||
slog.Warn("write failed", "what", what, "err", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user