I have the following language switcher as a partial included in both my header and footer. My issue is that the version used in the footer is generating different urls for the wrong pages. It looks pretty random. The one in the header is correct.
It’s the same code, the same context (current page). Yet it produces different urls somehow.
This somehow produces different results in the footer than in the header.
For example on my imprint page, I have the header correctly providing links to /imprint and /de/impint. And then the footer ends up having links to /categories and /de/categories.
Am I doing something wrong or am I somehow triggering a bug in hugo where it just randomly changes the context to whatever. This looks like the footer just ends up using the context of a completely different page. How is this even possible?
Is there a way to workaround this?
My hugo env:
hugo v0.123.8-5fed9c591b694f314e5939548e11cc3dcb79a79c+extended darwin/arm64 BuildDate=2024-03-07T13:14:42Z VendorInfo=brew
GOOS="darwin"
GOARCH="arm64"
GOVERSION="go1.22.1"
github.com/sass/libsass="3.6.5"
github.com/webmproject/libwebp="v1.3.2"
Without looking into the details, this seems a bit complicated and fragile. Is there some reason you aren’t just ranging over Page.Translations or Page.AllTranslations?
I tried the approach in the docs. Somehow .AllTranslations is empty on my page and .IsTranslated returns false; even though there is a translation. I’m not sure why but I guess it relates to something hugoplate is doing differently somehow.
They closed my bug as I adapted their theme; fair enough. This looks potentially like a hugo bug where some kind of caching related logic ends up swapping my page object for that of a completely different page.
Is there anything that I can look for that might trigger that? Clearly something happens in between the header and the footer where the footer is basically receiving a completely different object than the header. Kind of worrying that that can happen at all.
I actually gave the Translations one a try but it still generating different URLs when placed in the footer. On a new test site, it behaves as expected. I am still unsure how to narrow down the issue.
OK, I’ve cloned and built your site with hugo server. Which page should I visit, and how can I see that something’s wrong? And which of the three language switcher templates should I be looking at?
Guess the main reason is the different ways the hogoplate header and footer are included in baseof.html. They consider the footer to be a static one for each language. So they turn on caching of Partial for the footer in Production.
So I guess in Development it will work, but in Production you get unpredictable links to the transalted pages
The footer is cached without variant, so the caching is not based on Context. Related to the parallel processing of all that stuff with goroutines you may not predict which pages footer makes it into the cache.
Each Site (or language) has its own partialCached cache, so each site will execute a partial once.
Hugo renders pages in parallel, and will render the partial more than once with concurrent calls to the partialCached function. After Hugo caches the rendered partial, new pages entering the build pipeline will use the cached result.
Possible Solutions:
turn of caching in production
pass an additional variant to the footer call in baseof.html.
something like {{ partialCached "essentials/footer.html" . $ }}
dunno if you will also turn of that caching for script.html
Wow! I also added partialCached to the footer and removing it solves the issue! Might have been an oversight on my part when testing hugo --templateMetrics --templateMetricsHints since I made my own theme from scratch. I guess on Hugo Plate’s side, they should leave that decision to the user.