sitemap

package
v14.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Sitemap

Overview

The sitemap controller for the Iris web framework provides a mechanism to automatically generate and serve sitemap XML files based on the registered routes of your application. This controller helps improve your website's SEO by providing search engines with a structured list of URLs to crawl.

Features

  • Automatic Sitemap Generation: Generates sitemap XML files based on the registered routes.
  • Localization Support: Supports localized links for different languages.
  • Customizable URL Properties: Allows customization of URL properties such as lastmod, changefreq, and priority.
  • Sitemap Indexing: Automatically splits sitemaps and serves a sitemap index if more than 50,000 static routes are registered.

Usage

Installation

To use the Sitemap controller, you need to import it into your Iris application:

import (
	"github.com/kataras/iris/v14"
	"github.com/kataras/iris/v14/controller/sitemap"
)
Basic Usage

To enable the Sitemap controller, simply use it in your Iris application:

app := iris.New()
app.Router("/", sitemap.New("http://localhost"))
Localization Support

You can add localization support for the generated links in the sitemap by using the WithLocalization method:

import (
	"github.com/kataras/iris/v14/middleware/i18n"
)

app := iris.New()

// Load locales.
locales := i18n.New()
locales.Load("./locales/*/*", "en-US", "el-GR", "zh-CN")
locales.SetDefault("en-US")
i18nHandler := i18n.NewHandler(locales, i18n.HandlerOptions{
    // Retrieve the language from the URL parameter, e.g. "/?lang=el-GR".
    URLParameter: "lang", 
    // Retrieve the language from the cookie, e.g. "lang=el-GR".
    Cookie:       "lang",
})
app.Use(i18nHandler)

sitemapRouter := sitemap.New("http://localhost").WithLocalization(i18nHandler, nil)
app.Router("/", sitemapRouter)

Example

Here is a complete example demonstrating the usage of the Sitemap controller:

package main

import (
	"github.com/kataras/iris/v14"
	"github.com/kataras/iris/v14/controller/sitemap"
)

func main() {
	app := iris.New()
	app.Router("/", sitemap.New("http://localhost"))

	app.Get("/", func(ctx iris.Context) {
		ctx.WriteString("Index")
	})

	app.Get("/about", func(ctx iris.Context) {
		ctx.WriteString("About")
	})

	app.Get("/signup", func(ctx iris.Context) {
		ctx.WriteString("Signup Form")
	})
	app.Post("/signup", func(ctx iris.Context) {
		ctx.WriteString("Signup")
	})

	app.Listen(":8080")
}
Example Output

When you access the /sitemap.xml endpoint, the generated sitemap XML will look like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://localhost/</loc>
  </url>
  <url>
    <loc>http://localhost/about</loc>
  </url>
  <url>
    <loc>http://localhost/signup</loc>
  </url>
</urlset>

The Sitemap controller reads the registered routes and generates the sitemap XML from them. It excludes dynamic routes, subdomain routes, offline routes, and any route that called ExcludeSitemap.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

Controller is a router.Controller which can be used to generate and register sitemap(%d).xml endpoint(s) based on the registered routes.

func New

func New(domain string) *Controller

New returns a new sitemap router. It accepts the domain name as its only parameter. Use the Route's `SetLastMod`, `SetChangeFreq` and `SetPriority` to modify the sitemap's URL child element properties . Excluded routes: - dynamic - subdomain - offline - ExcludeSitemap method called

The "domain" argument is the prefix for the registered routes that will be included in the sitemap.

If more than 50,000 static routes are registered then sitemaps will be splitted and a sitemap index will be served in /sitemap.xml.

If `Application.I18n.Load/LoadAssets` is called then the sitemap will contain translated links for each static route.

If the result does not complete your needs you can take control and use the github.com/kataras/sitemap package to generate a customized one instead. Usage:

app := iris.New()
[Register other routes first...]
app.Router("/", sitemap.New("https://www.example.com"))

func (*Controller) Routing

func (s *Controller) Routing(r router.Router)

Routing implements the router.Controller interface.

func (*Controller) WithLocalization

func (s *Controller) WithLocalization(handler *i18n.Handler, router *i18n.Router) *Controller

WithLocalization adds localization support for the generated links in the sitemap, specify the i18n handler or router or both.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL