Developers · Best Practices
Fragment Cache
Last updated 2 November 2026
You can cache specific parts of a page or layout using fragment caching. An example of an everyday use-case scenario is to cache fragments that take a lot of time to fetch or render, for example, a long-running GraphQL query.
Testing the Cache
You can test the cache by adding a random string and refreshing the page:
- The string remains the same when the page is rendered from the cache.
- The string changes when the cache is invalidated (e.g. after the time specified (in seconds) in the expire property).
app/views/pages/fragment-cache-simple.liquid
{% cache context.page.slug, expire: 10 %}
Random string: {{ 18 | random_string }}
{% endcache %}Example
This example demonstrates rendering a lot of data. In this case the data is generated inline. In the real world, this would be fetched from the database:
app/views/pages/fragment-cache-long-running.liquid
Random string not cached: {{ 18 | random_string }} <br>
{% capture key %}{{ context.page.slug }}-v1{% endcapture %}
{% cache key, expire: 15 %}
Random string cached: {{ 18 | random_string }} <br>
{%- for i in (1..5000) -%}
{%- assign r = 15 | random_string %}
<span class='{{ r }}'>{{ i }}</span>
{%- endfor -%}
{% endcache %}Authentication and Cross-Site Request Forgery Tokens
If you cache a fragment that includes a token (like an
Insites automatically processes the cache and updates those tokens if you use the form Liquid tag.
If you're using JavaScript to submit forms, you can obtain the CSRF token for the current page from a cookie named