Hello, I have a website with 40k pages. The site has two menus. one for mobile and one for desktop. each menu is generated in its own partial with the bellow same code on both partials:
{{ $type := .Type }}
{{ $kind := .Kind }}
{{ $currentPage := . }}
{{ range .Site.Menus.main }}
<li class="menu-item {{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }} is-active{{ end }}{{ if and (eq .Identifier "home") (eq $type "post") }} is-active{{ end }}{{ if and (eq .Identifier "home") (and (eq $type "page") (eq $kind "taxonomy"))}} is-active{{ end }}">
<a href="{{ .URL }}">{{ .Name }}</a>
</li>
{{ end }}
Is there any way to save this in a variable and then pass it to each partial Or any other workaround to prevent the generation of exact same menu 2 times.
Check out the “variants” feature of partialCached. Or I would call it cache tags and not variants.
You would need to use the same partial for both menus and add the current page as the variant. I think that would make the second invocation use the cache.
Eventually I did it with javascript and cache completely the partial
(function () {
var current = location.pathname.split('/')[1];
if (current === "") return;
var menuItems = document.querySelectorAll('.menu-item a');
for (var i = 0, len = menuItems.length; i < len; i++) {
if (menuItems[i].getAttribute("href").indexOf(current) !== -1) {
menuItems[i].className = "is-active";
}
}
})();