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.
1
app := iris.New()
2
// [...]
3
4
app.Listen(":8080", iris.WithSitemap("http://localhost:8080"))
Copied!
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.
1
app.Get("/home", handler).SetLastMod(time.Now()).SetChangeFreq("hourly").SetPriority(0.8)
Copied!
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:
1
package main
2
3
import (
4
"time"
5
6
"github.com/kataras/iris/v12"
7
)
8
9
const startURL = "http://localhost:8080"
10
11
func main() {
12
app := newApp()
13
14
// http://localhost:8080/sitemap.xml
15
// Lists only online GET static routes.
16
//
17
// Reference: https://www.sitemaps.org/protocol.html
18
app.Listen(":8080", iris.WithSitemap(startURL))
19
}
20
21
func newApp() *iris.Application {
22
app := iris.New()
23
app.Logger().SetLevel("debug")
24
25
lastModified, _ := time.Parse("2006-01-02T15:04:05-07:00", "2019-12-13T21:50:33+02:00")
26
app.Get("/home", handler).SetLastMod(lastModified).SetChangeFreq("hourly").SetPriority(1)
27
app.Get("/articles", handler).SetChangeFreq("daily")
28
app.Get("/path1", handler)
29
app.Get("/path2", handler)
30
31
app.Post("/this-should-not-be-listed", handler)
32
app.Get("/this/{myparam}/should/not/be/listed", handler)
33
app.Get("/this-should-not-be-listed-offline", handler).SetStatusOffline()
34
35
// These should be excluded as well
36
app.Get("/about", handler).ExcludeSitemap()
37
app.Get("/offline", handler).SetStatusOffline()
38
39
return app
40
}
41
42
func handler(ctx iris.Context) { ctx.WriteString(ctx.Path()) }
Copied!

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.
1
GET http://localhost:8080/sitemap.xml
Copied!
1
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
2
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
3
<url>
4
<loc>http://localhost:8080/</loc>
5
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/"></xhtml:link>
6
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/"></xhtml:link>
7
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/"></xhtml:link>
8
</url>
9
<url>
10
<loc>http://localhost:8080/some-path</loc>
11
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/some-path"></xhtml:link>
12
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/some-path"></xhtml:link>
13
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/some-path"></xhtml:link>
14
</url>
15
<url>
16
<loc>http://localhost:8080/other</loc>
17
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/other"></xhtml:link>
18
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/other"></xhtml:link>
19
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/other"></xhtml:link>
20
</url>
21
<url>
22
<loc>http://localhost:8080/templates</loc>
23
<xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:8080/templates"></xhtml:link>
24
<xhtml:link rel="alternate" hreflang="el-GR" href="http://localhost:8080/el-GR/templates"></xhtml:link>
25
<xhtml:link rel="alternate" hreflang="zh-CN" href="http://localhost:8080/zh-CN/templates"></xhtml:link>
26
</url>
27
</urlset>
Copied!
Last modified 1yr ago
Export as PDF
Copy link
Contents
Localization