From 3935faa4175940dbd56b4e3a023163623b4bbdf2 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Fri, 15 Mar 2024 15:15:24 -0700 Subject: [PATCH] hugolib: Fix sitemap index with monolingual site Fixes #12266 --- hugolib/site.go | 2 +- hugolib/sitemap_test.go | 57 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/hugolib/site.go b/hugolib/site.go index e7d170d09..2275dbe84 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -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) diff --git a/hugolib/sitemap_test.go b/hugolib/sitemap_test.go index 6dad39fe3..a5a94c67e 100644 --- a/hugolib/sitemap_test.go +++ b/hugolib/sitemap_test.go @@ -15,6 +15,7 @@ package hugolib import ( "reflect" + "strings" "testing" "github.com/gohugoio/hugo/config" @@ -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"] @@ -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", + "https://example.org/de/sitemap.xml", + "https://example.org/en/sitemap.xml", + ) + b.AssertFileContent("public/de/sitemap.xml", "https://example.org/de/") + b.AssertFileContent("public/en/sitemap.xml", "https://example.org/en/") + + // Test B: multilingual with defaultContentLanguageInSubdir = false + files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = true", "defaultContentLanguageInSubdir = false") + + b = Test(t, files) + + b.AssertFileContent("public/sitemap.xml", + "https://example.org/de/sitemap.xml", + "https://example.org/en/sitemap.xml", + ) + b.AssertFileContent("public/de/sitemap.xml", "https://example.org/de/") + b.AssertFileContent("public/en/sitemap.xml", "https://example.org/") + + // 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", "https://example.org/") + + // Test D: monolingual with defaultContentLanguageInSubdir = true + files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = false", "defaultContentLanguageInSubdir = true") + + b = Test(t, files) + + b.AssertFileContent("public/sitemap.xml", "https://example.org/en/sitemap.xml") + b.AssertFileContent("public/en/sitemap.xml", "https://example.org/en/") +}