feat: HTML sitemap groups API

- GET /_sitemap/api/v1/html?locale= answers the overview: the listed
  addresses of the XML sitemaps by group, in the order pages, offers,
  services, categories (as a tree), posts (newest first), tags, authors,
  other content types, each with its title in the locale, total, the
  first html_page_size links and more
- GET /_sitemap/api/v1/html/{group}?locale=&page= answers one page of a
  group; past the last page, an unknown group or the HTML sitemap off
  answer 404; every answer is noindex; per-IP bucket sitemap-html
- the HTML sitemap tab: html_enabled, the address per locale (html_paths)
  and html_page_size 50 to 500, with errors.html_path and errors.page_size
- contract.Listing.Settings answers HTMLEnabled, HTMLPaths, HTMLPageSize
  and KeyFile
This commit is contained in:
Jakub Zych
2026-11-04 12:32:47 +01:00
parent e02d0b173f
commit 8f4ff2a97d
12 changed files with 981 additions and 67 deletions

View File

@@ -21,8 +21,12 @@ const (
cacheControl = "no-cache, private"
)
// BucketSitemap is the throttle bucket of the sitemap routes.
const BucketSitemap = "sitemap-xml"
// The throttle buckets: BucketSitemap of the sitemap files and the key
// file, BucketHTML of the HTML sitemap API.
const (
BucketSitemap = "sitemap-xml"
BucketHTML = "sitemap-html"
)
// FilesPath is the route of the per-group files. The index lists them at
// the site root (sitemap-<slug>-<n>.xml); the site's SEO layer or proxy
@@ -33,36 +37,49 @@ const FilesPath = "/_sitemap/files/"
// (one file) and GET /_sitemap/indexnow/{file} (the IndexNow key file)
// without the backend middleware: search engines fetch them anonymously.
// The sitemaps are served from the slot table, never generated per
// request, and every route is throttled per client IP (BucketSitemap).
// request, and every route is throttled per client IP (BucketSitemap). GET
// /_sitemap/api/v1/html and /_sitemap/api/v1/html/{group} answer the HTML
// sitemap's data (BucketHTML).
func (p *Plugin) Routes(r pact.Router) error {
r.Get("/sitemap.xml", p.sitemapIndex, "throttle:"+BucketSitemap)
r.Get("/_sitemap/files/{file}", p.sitemapFile, "throttle:"+BucketSitemap)
r.Where("file", `sitemap-(news|[a-z0-9-]+-[0-9]+)\.xml`)
r.Get("/_sitemap/indexnow/{file}", apicontrollers.KeyFile(p.booted), "throttle:"+BucketSitemap)
r.Where("file", apicontrollers.KeyFilePattern)
// The HTML sitemap's data (D-34), read by the site's server when it
// renders the HTML sitemap page; public, noindex, per-IP bucket
// sitemap-html.
r.Get("/_sitemap/api/v1/html", apicontrollers.HTMLOverview(p.booted), "throttle:"+BucketHTML)
r.Get("/_sitemap/api/v1/html/{group}", apicontrollers.HTMLGroup(p.booted), "throttle:"+BucketHTML)
r.Where("group", apicontrollers.GroupPattern)
return nil
}
// booted returns the booted application.
func (p *Plugin) booted() *backpack.App { return p.app }
// Buckets registers sitemap-xml: 60 requests a minute per client IP (behind
// the configured trusted proxies). 429 bodies are surf's
// {"message":"Too Many Attempts."}.
// Buckets registers sitemap-xml (60 requests a minute) and sitemap-html
// (300 a minute: the site's server renders every HTML sitemap page from
// it) per client IP, behind the configured trusted proxies. 429 bodies are
// surf's {"message":"Too Many Attempts."}.
func (p *Plugin) Buckets() map[string]surf.Bucket {
trusted := surf.TrustedProxies(nil)
if p.app != nil {
trusted = surf.TrustedProxies(p.app.Config)
}
return map[string]surf.Bucket{
BucketSitemap: {
Name: BucketSitemap,
Max: 60,
bucket := func(name string, max int) surf.Bucket {
return surf.Bucket{
Name: name,
Max: max,
Decay: time.Minute,
Key: func(r *http.Request) string {
return "sitemap-xml|" + surf.ClientIP(r, trusted)
return name + "|" + surf.ClientIP(r, trusted)
},
},
}
}
return map[string]surf.Bucket{
BucketSitemap: bucket(BucketSitemap, 60),
BucketHTML: bucket(BucketHTML, 300),
}
}