gohugoio / hugo

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

RelPermalink incorrect for scss in combination with canonifyURLs #12774

Open caspermeijn opened 1 month ago

caspermeijn commented 1 month ago

The RelPermalink of scss file converted using toCSS is incorrect when the baseURL has a path and canonifyURLs is enabled.

I already found https://github.com/gohugoio/hugo/issues/4733 which suggests removing support of canonifyURLs. I will disable canonifyURLs in my configuration to work around this problem.

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.132.1-1bde700dfc0770bb11eb8445aff1ab5abdccb46e+extended linux/amd64 BuildDate=2024-08-13T10:10:10Z VendorInfo=gohugoio

Does this issue reproduce with the latest release?

Yes, it does happen with v0.132.2.

Reproducable

I created a test that can reproduce the problem. This test fails when added to the hugo codebase.


func TestRelParmalink(t *testing.T) {

    files := `
-- hugo.toml --
canonifyURLs = true
baseURL = 'https://caspermeijn.github.io/receptenboek/'
-- assets/scss/main.scss --
h2 {
    color: blue;
}
-- layouts/index.html --
{{ $cssOpts := (dict "targetPath" "css/gochowdown.css" ) }}
{{ $r := resources.Get "scss/main.scss" |  toCSS $cssOpts }}
<link rel="stylesheet" href="{{ $r.RelPermalink }}">
        `

    b := hugolib.NewIntegrationTestBuilder(
        hugolib.IntegrationTestConfig{
            T:           t,
            TxtarString: files,
            NeedsOsFS:   true,
        }).Build()

    b.AssertFileContent("public/index.html", "<link rel=\"stylesheet\" href=\"https://caspermeijn.github.io/receptenboek/css/gochowdown.css\">")
}
jmooring commented 1 month ago

In the example above...

Expected URL to stylesheet:

https://caspermeijn.github.io/receptenboek/css/gochowdown.css

Actual URL to stylesheet:

https://caspermeijn.github.io/receptenboek/receptenboek/css/gochowdown.css

Instead of fixing this, can we instead deprecate canonifyURLs per https://github.com/gohugoio/hugo/issues/4733? In the documentation we've been discouraging its use for a quite a while: https://gohugo.io/content-management/urls/#canonical-urls

caspermeijn commented 1 month ago

Seems a valid solution to me. I was using canonifyURLs because it came with the example inside a theme. I didn't know it was discouraged.