Hugo: partialCached or not?

hugo:-partialcached-or-not?

The name is pretty self-explanatory. It caches the render of a given partial.

While it can optimize the build, especially when you have to deal with heavy templates and complex calculations, partial and partialCached keywords are not interchangeable.

Basic usage

The most common implementation would be similar to the following:

{{ partialCached "footer.html" . }}

It assumes the partial generates the same code every time.

More advanced usage

The documentation you can pass additional arguments:

{{ partialCached "footer.html" . .Section }}

Although, you might miss the point if you don’t read the documentation carefully:

The variant arguments are not available to the underlying partial template; they are only used to create unique cache keys

Those arguments are only used to create unique cache keys.

Everything wrong with cached partials

  • ❌ don’t use partialCached if there are too much variants (e.g., one per page)
  • ❌ don’t forget the context (2nd argument)
  • ❌ don’t try to use the arguments within the partial itself
  • ❌ don’t use partialCached for very small websites (e.g., one-page, portfolio)

Better practices

  • ✅ remember Hugo executes the partial only once per key
  • ✅ use partialCached when the content is the same on all pages or, at least, several pages (e.g., sections, footer)
  • ✅ replace partialCached with partial if you encounter some performance issues or display errors
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
sunday-rewind:-top-7-signs-you’re-in-a-bad-product-system-by-scott-williamson

Sunday Rewind: Top 7 signs you’re in a bad product system by Scott Williamson

Next Post
what-are-http-status-codes-and-why-do-they-matter?

What Are HTTP Status Codes and Why Do They Matter?

Related Posts

TestSprite — localized dev review with feedback

TestSprite 本地化开发评测:一次真实项目的集成与踩坑记录(含截图与代码) 平台建议发布:CSDN / DEV.to / Medium / 个人博客 评测类型:开发者视角 + 本地化(i18n / l10n)专项检查 项目背景:一个面向国内与海外用户的管理后台(React + Node.js),包含订单金额、时间范围筛选、多语言切换、CSV 导出等功能。…
Read More