Sitemap
Iris provides extensive support for the Sitemap Protocol which automatically generates sitemap index "/sitemap.xml" too.
To enable sitemaps on your web application you should use the iris.WithSitemap
Configurator. This function accepts the full scheme and domain of the exposed application.
app := iris.New()
// [...]
app.Listen(":8080", iris.WithSitemap("http://localhost:8080"))
The application will loop through registered static routes and it will add each one of them to the sitemap file. By default only the <loc>
XML element will be filled unless the route's fields LastMod
, ChangeFreq
or/and Priority
* are set.
app.Get("/home", handler).SetLastMod(time.Now()).SetChangeFreq("hourly").SetPriority(0.8)
A static route is exposed on GET HTTP Method and its path does not contain a dynamic parameter. e.g. /home, /about but not /user/{id:uint64} and e.t.c.
Example Code:
package main
import (
"time"
"github.com/kataras/iris/v12"
)
const startURL = "http://localhost:8080"
func main() {
app := newApp()
// http://localhost:8080/sitemap.xml
// Lists only online GET static routes.
//
// Reference: https://www.sitemaps.org/protocol.html
app.Listen(":8080", iris.WithSitemap(startURL))
}
func newApp() *iris.Application {
app := iris.New()
app.Logger().SetLevel("debug")
lastModified, _ := time.Parse("2006-01-02T15:04:05-07:00", "2019-12-13T21:50:33+02:00")
app.Get("/home", handler).SetLastMod(lastModified).SetChangeFreq("hourly").SetPriority(1)
app.Get("/articles", handler).SetChangeFreq("daily")
app.Get("/path1", handler)
app.Get("/path2", handler)
app.Post("/this-should-not-be-listed", handler)
app.Get("/this/{myparam}/should/not/be/listed", handler)
app.Get("/this-should-not-be-listed-offline", handler).SetStatusOffline()
// These should be excluded as well
app.Get("/about", handler).ExcludeSitemap()
app.Get("/offline", handler).SetStatusOffline()
return app
}
func handler(ctx iris.Context) { ctx.WriteString(ctx.Path()) }
Localization
Sitemap translations are automatically set to each route by path prefix if app.I18n.PathRedirect
is true or by subdomain if app.I18n.Subdomain
is true or by URL query parameter if app.I18n.URLParameter
is not empty.
Read more at: https://support.google.com/webmasters/answer/189077?hl=en
GET http://localhost:8080/sitemap.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://localhost:8080/</loc>
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/"></xhtml:link>
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/"></xhtml:link>
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/"></xhtml:link>
</url>
<url>
<loc>http://localhost:8080/some-path</loc>
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/some-path"></xhtml:link>
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/some-path"></xhtml:link>
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/some-path"></xhtml:link>
</url>
<url>
<loc>http://localhost:8080/other</loc>
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/other"></xhtml:link>
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/other"></xhtml:link>
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/other"></xhtml:link>
</url>
<url>
<loc>http://localhost:8080/templates</loc>
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/templates"></xhtml:link>
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/templates"></xhtml:link>
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/templates"></xhtml:link>
</url>
</urlset>