gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
73.52k stars 7.38k forks source link

Resource Permalink returns incorrect base URL with multihost site #12560

Open jmooring opened 1 month ago

jmooring commented 1 month ago

This isn't anything new. I am able to reproduce with v0.54.0 and later.

func TestFoo(t *testing.T) {
    t.Parallel()

    files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
[languages.en]
baseURL = 'https://en.example.org/'
weight = 1
[languages.de]
baseURL = 'https://de.example.org/'
weight = 2
-- layouts/index.html --
{{ with resources.Get "main.css" }}{{ .Permalink }}{{ end }}
-- assets/main.css --
body {color: red;}
`

    b := hugolib.Test(t, files)

    b.AssertFileContent("public/en/main.css", "body {color: red;}")
    b.AssertFileContent("public/en/index.html", "https://en.example.org/main.css")
    b.AssertFileContent("public/de/main.css", "body {color: red;}")
    b.AssertFileContent("public/de/index.html", "https://de.example.org/main.css") // fails
}

The last assertion fails; the file is identical to public/en/index.html, which means it's pointing to the wrong site.

Reference: https://discourse.gohugo.io/t/multihost-trouble/50103

The work around is to use RelPermalink instead:

{{ with resources.Get "main.css" }}
  {{ .RelPermalink }}
{{ end }}
adityatelange commented 4 weeks ago

The following also seems related?

bep commented 4 weeks ago

Thinking back, I'm not sure I would label this as a bug. Resources inside /assets are considered global and we have never had any concept of "duplicating resources from assets" across languages. So, I think this is "as designed" -- whether the design is great, that cold be debated.

jmooring commented 4 weeks ago

In a multihost configuration we are duplicating resources from assets across languages. The test case above produces this:

public/
├── de/
│   ├── index.html
│   └── main.css
└── en/
    ├── index.html
    └── main.css

The problem is that, for one of the sites, the .Permalink method points to the wrong place.