]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
hugolib: Fix sitemap index with monolingual site
authorJoe Mooring <joe.mooring@veriphor.com>
Fri, 15 Mar 2024 22:15:24 +0000 (15:15 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 16 Mar 2024 10:49:00 +0000 (11:49 +0100)
Fixes #12266

hugolib/site.go
hugolib/sitemap_test.go

index e7d170d093722d9e77bac3a025b428f85e41fa81..2275dbe8415ee5cbdad752ed81c62d6d23db2541 100644 (file)
@@ -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)
index 6dad39fe33926b198515b69f95317b4e8f5231df..a5a94c67e75763e6e131ea703f68bd7f02c80902 100644 (file)
@@ -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",
+               "<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>")
+}