A Jekyll plugin to cache the rendering of Liquid includes
The Jekyll Include Cache is a plugin that helps improve the performance of Jekyll websites by caching the rendering of computationally expensive Liquid includes. By rendering the include once and reusing the output whenever it is called with the same arguments, the plugin can potentially speed up the build process of the site significantly.
To install and use the Jekyll Include Cache plugin, follow these steps:
Add the following line to your site’s Gemfile:
gem 'jekyll-include-cache'
Add the following code snippet to your site’s config file (usually _config.yml):
plugins:
- jekyll-include-cache
Note: If you are using a Jekyll version less than 3.5.0, use the gems key instead of plugins.
Replace the syntax for including a Liquid file in your template. Change:
{% include foo.html %}
to:
{% include_cached foo.html %}
This will instruct Jekyll to use the cached version of the include.
Make sure to pass all required variables to your include as arguments and reference them within the include using the include.variable syntax. For example:
Good:
In your template:
{% assign foo = "bar" %}
{% include_cached foo.html foo=foo %}
In your include:
{{/* include.foo */}}
Bad:
In your template:
{% assign foo = "bar" %}
{% include_cached foo.html %}
In your include:
{{/* page.foo */}}
or
{{/* foo */}}
The Jekyll Include Cache plugin offers a solution for improving the performance of Jekyll websites by caching the rendering of computationally expensive Liquid includes. By following the installation steps and making necessary adjustments to include syntax and variable referencing, developers can achieve significant improvements in the build speed of their websites.