partialCache variants

I’m trying to implement some caching, but I’m confused by how the variant variable works.

So for example,​ I have the line {{ partialCache "header" . .Section }} then I run into problems since the header partial has a line which uses .IsPage to decide whether certain JS/CSS is used.

When using partialCache it seems .IsPage always returns false, though I thought the .Section variant means the cache is rebuilt for each section (e.g. posts or sermons in this theme). Have I missed something in the docs?

Speculating here, but /posts/ and /posts/foo/ will both have posts as its value for .Section. So both /posts/and /posts/foo/ will have the same partial cached “value” despite one being .IsPage: true and the other not.

1 Like

Thanks @pointyfar, you’re probably right. I think the logic should be using .IsHome. I’ll give that a try.

So, I’ve been playing with this. What other options can be passed as a variant to partialCached? Is there a way to get a the cache to be reused for all the Pages in a section (i.e. built with single.html) and not for the Node (list.html).

Basically, the above code snippet is the use case: can I use partialCached for building all the individual pages/posts in a section, but not use the cached version for the list or homepage parts of the site. Or am I trying to make things too tricky, and hugo’s efficiency means I don’t need to worry too much about making maximal use of caching?

Maybe you could try something like

{{ if .IsPage }}
  {{ partialCached ... }}
{{ else }}
  {{ partial ... }}
{{ end }}

so it only uses the cached version on regular pages. Personally untested though.

partialCached can take any number of variants (strings only) so:

{{ partialCached "header.html" . .Section "list" }}
{{ partialCached "header.html" . .Section "single" }}

should, I think solve your problem

Thanks @regis and @pointyfar. I’ve combined both of those suggestions to have a little bit of logic in _default/baseof.html and it seems to work!

(For some reason discourse has cached the code displayed here…but click through and you’ll see what I’ve done.)