Skip to content

Commit

Permalink
hugolib: Fix sitemap index with monolingual site
Browse files Browse the repository at this point in the history
Fixes #12266
  • Loading branch information
jmooring authored and bep committed Mar 16, 2024
1 parent d4d49e0 commit 3935faa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hugolib/site.go
Expand Up @@ -601,7 +601,7 @@ func (h *HugoSites) fileEventsContentPaths(p []pathChange) []pathChange {
// HomeAbsURL is a convenience method giving the absolute URL to the home page.
func (s *Site) HomeAbsURL() string {
base := ""
if len(s.conf.Languages) > 1 {
if len(s.conf.Languages) > 1 || s.Conf.DefaultContentLanguageInSubdir() {
base = s.Language().Lang
}
return s.AbsURL(base, false)
Expand Down
57 changes: 56 additions & 1 deletion hugolib/sitemap_test.go
Expand Up @@ -15,6 +15,7 @@ package hugolib

import (
"reflect"
"strings"
"testing"

"github.com/gohugoio/hugo/config"
Expand Down Expand Up @@ -127,7 +128,7 @@ func TestParseSitemap(t *testing.T) {
func TestSitemapShouldNotUseListXML(t *testing.T) {
t.Parallel()

files := `
files := `
-- hugo.toml --
baseURL = "https://example.com"
disableKinds = ["term", "taxonomy"]
Expand Down Expand Up @@ -170,3 +171,57 @@ type: sitemap

b.AssertFileExists("public/sitemap.xml", true)
}

// Issue 12266
func TestSitemapIssue12266(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
baseURL = 'https://example.org/'
disableKinds = ['rss','taxonomy','term']
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
[languages.de]
[languages.en]
`

// Test A: multilingual with defaultContentLanguageInSubdir = true
b := Test(t, files)

b.AssertFileContent("public/sitemap.xml",
"<loc>https://example.org/de/sitemap.xml</loc>",
"<loc>https://example.org/en/sitemap.xml</loc>",
)
b.AssertFileContent("public/de/sitemap.xml", "<loc>https://example.org/de/</loc>")
b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/en/</loc>")

// Test B: multilingual with defaultContentLanguageInSubdir = false
files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = true", "defaultContentLanguageInSubdir = false")

b = Test(t, files)

b.AssertFileContent("public/sitemap.xml",
"<loc>https://example.org/de/sitemap.xml</loc>",
"<loc>https://example.org/en/sitemap.xml</loc>",
)
b.AssertFileContent("public/de/sitemap.xml", "<loc>https://example.org/de/</loc>")
b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/</loc>")

// Test C: monolingual with defaultContentLanguageInSubdir = false
files = strings.ReplaceAll(files, "[languages.de]", "")
files = strings.ReplaceAll(files, "[languages.en]", "")

b = Test(t, files)

b.AssertFileExists("public/en/sitemap.xml", false)
b.AssertFileContent("public/sitemap.xml", "<loc>https://example.org/</loc>")

// Test D: monolingual with defaultContentLanguageInSubdir = true
files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = false", "defaultContentLanguageInSubdir = true")

b = Test(t, files)

b.AssertFileContent("public/sitemap.xml", "<loc>https://example.org/en/sitemap.xml</loc>")
b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/en/</loc>")
}

0 comments on commit 3935faa

Please sign in to comment.