Sitemap indices with LastMod

This commit is contained in:
Jan-Lukas Else 2021-08-18 14:47:31 +02:00
parent b78c044020
commit f902beffd3
1 changed files with 11 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"database/sql" "database/sql"
"encoding/xml" "encoding/xml"
"net/http" "net/http"
"time"
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/snabb/sitemap" "github.com/snabb/sitemap"
@ -23,9 +24,11 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
// Create sitemap // Create sitemap
sm := sitemap.NewSitemapIndex() sm := sitemap.NewSitemapIndex()
// Add blog sitemap indices // Add blog sitemap indices
now := time.Now().UTC()
for _, bc := range a.cfg.Blogs { for _, bc := range a.cfg.Blogs {
sm.Add(&sitemap.URL{ sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(sitemapBlogPath)), Loc: a.getFullAddress(bc.getRelativePath(sitemapBlogPath)),
LastMod: &now,
}) })
} }
// Write sitemap // Write sitemap
@ -37,14 +40,18 @@ func (a *goBlog) serveSitemapBlog(w http.ResponseWriter, r *http.Request) {
sm := sitemap.NewSitemapIndex() sm := sitemap.NewSitemapIndex()
// Add blog sitemaps // Add blog sitemaps
b := r.Context().Value(blogKey).(string) b := r.Context().Value(blogKey).(string)
now := time.Now().UTC()
sm.Add(&sitemap.URL{ sm.Add(&sitemap.URL{
Loc: a.getFullAddress(a.getRelativePath(b, sitemapBlogFeaturesPath)), Loc: a.getFullAddress(a.getRelativePath(b, sitemapBlogFeaturesPath)),
LastMod: &now,
}) })
sm.Add(&sitemap.URL{ sm.Add(&sitemap.URL{
Loc: a.getFullAddress(a.getRelativePath(b, sitemapBlogArchivesPath)), Loc: a.getFullAddress(a.getRelativePath(b, sitemapBlogArchivesPath)),
LastMod: &now,
}) })
sm.Add(&sitemap.URL{ sm.Add(&sitemap.URL{
Loc: a.getFullAddress(a.getRelativePath(b, sitemapBlogPostsPath)), Loc: a.getFullAddress(a.getRelativePath(b, sitemapBlogPostsPath)),
LastMod: &now,
}) })
// Write sitemap // Write sitemap
a.writeSitemapXML(w, sm) a.writeSitemapXML(w, sm)