MCPcopy Create free account
hub / github.com/kataras/iris / WithSitemap

Function WithSitemap

configuration.go:513–605  ·  view source on GitHub ↗

WithSitemap enables the sitemap generator. 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 It accepts a "startURL" input argument which is the prefix

(startURL string)

Source from the content-addressed store, hash-verified

511//
512// Example: https://github.com/kataras/iris/tree/main/_examples/sitemap.
513func WithSitemap(startURL string) Configurator {
514 sitemaps := sitemap.New(startURL)
515 return func(app *Application) {
516 var defaultLang string
517 if tags := app.I18n.Tags(); len(tags) > 0 {
518 defaultLang = tags[0].String()
519 sitemaps.DefaultLang(defaultLang)
520 }
521
522 for _, r := range app.GetRoutes() {
523 if !r.IsStatic() || r.Subdomain != "" || !r.IsOnline() || r.NoSitemap {
524 continue
525 }
526
527 loc := r.StaticPath()
528 var translatedLinks []sitemap.Link
529
530 for _, tag := range app.I18n.Tags() {
531 lang := tag.String()
532 langPath := lang
533 href := ""
534 if lang == defaultLang {
535 // http://domain.com/en-US/path to just http://domain.com/path if en-US is the default language.
536 langPath = ""
537 }
538
539 if app.I18n.PathRedirect {
540 // then use the path prefix.
541 // e.g. http://domain.com/el-GR/path
542 if langPath == "" { // fix double slashes http://domain.com// when self-included default language.
543 href = loc
544 } else {
545 href = "/" + langPath + loc
546 }
547 } else if app.I18n.Subdomain {
548 // then use the subdomain.
549 // e.g. http://el.domain.com/path
550 scheme := netutil.ResolveSchemeFromVHost(startURL)
551 host := strings.TrimLeft(startURL, scheme)
552 if langPath != "" {
553 href = scheme + strings.Split(langPath, "-")[0] + "." + host + loc
554 } else {
555 href = loc
556 }
557
558 } else if p := app.I18n.URLParameter; p != "" {
559 // then use the URL parameter.
560 // e.g. http://domain.com/path?lang=el-GR
561 href = loc + "?" + p + "=" + lang
562 } else {
563 // then skip it, we can't generate the link at this state.
564 continue
565 }
566
567 translatedLinks = append(translatedLinks, sitemap.Link{
568 Rel: "alternate",
569 Hreflang: lang,
570 Href: href,

Callers

nothing calls this directly

Calls 15

ResolveSchemeFromVHostFunction · 0.92
NewMethod · 0.80
URLMethod · 0.80
AddRouteUnsafeMethod · 0.80
ErrorfMethod · 0.80
TagsMethod · 0.65
StringMethod · 0.65
GetRoutesMethod · 0.65
IsStaticMethod · 0.65
IsOnlineMethod · 0.65
StaticPathMethod · 0.65
BuildMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…