Any idea why this code wouldn’t update/break the cache when new items are created?

Think perhaps the combination of my code for pagination, cache and the ability to also break for pending entries isn't correct and throwing it off...

 # ---------------

{% extends "_layouts/main" %}

{# Get the most soonest upcoming pending entry for Cache breaking #}
{% set firstPending = craft.entries({
  section: 'news',
  status: 'pending',
  order: 'postDate asc'
}).first %}
{% set cacheUntil = firstPending ? firstPending.postDate : now|date_modify('+1 year') %}

{% set entries = craft.entries({
  section: 'news',
  order: 'postDate desc',
  limit: 6
}) %}

{# Get the articles and paginate them #}
{% paginate entries as pageInfo, pageEntries %}

{% block main %}
{% cache globally using key craft.request.path until cacheUntil %}
<div class="sitecontainer">
  <h1 class="border-bottom">{{ entry.title }}</h1>

  {% for entry in pageEntries %}
  <article class="leading-normal mb-8">
    <div class="sm:flex sm:items-center mb-8 pb-6 {% if not loop.last %}border-b border-grey-light{% endif %}">
      <div class="bg-petrol relative text-lime p-4 sm:w-40">
         <time datetime="{{ entry.postDate|date('Y-m-d H:i:s') }}" class=" text-center mb-0 relative z-30 text-sm">
          <div class="mx-auto fill-current w-6 h-6 mb-2">
            {% include '_includes/svg/icon-date.svg' %}
            {{ helpers.dateFormatted(entry.postDate) }}
      <div class="p-4 flex-1">
        <h2 class="flex // flex-wrap items-center border-grey // text-base leading-normal font-semibold mb-1">
          <a href="{{ entry.url }}">
            {{ entry.title }}
        <p class="mb-0 text-base">
          <a href="{{ entry.url }}" class="arrow font-normal text-lime hover:text-blue">view news</a>
  {% endfor %}
{% endcache %}

{% include '_includes/partials/pagination' %}
{% endblock %}

1 Answer 1


One issue I'm seeing, is that you're currently using craft.request.path as your cache key – however, that property doesn't actually include the pagination segment (i.e. the pageTrigger; p2, p3 or the like), which means that you're currently going to see the same, cached content across all of your pages, because you're caching globally using an identical key (i.e. the root URI of your paginated template).

For Craft 2, AFAIK there's no single property or method on the HttpRequestService that will give you the full URL including the pagination segment – so you'll need to append the pagination number using craft.request.getPageNum():

{% cache globally using key craft.request.path ~ craft.request.getPageNum() until cacheUntil %}

For Craft 3, there's craft.app.request.getFullPath(), which does include the pagination segment – so the following should do the trick:

{% cache globally using key craft.app.request.getFullPath() until cacheUntil %}

As far as your solution for breaking the cache whenever "pending" entries are published, I'm not sure, but I'm not seeing any problems with it, at a glance.

The reason why you're not seeing the cache cleared when you create new entries, is probably because your second craft.entries query (the one that actually fetches the entries) is outside your {% cache %} tag (this would be a performance issue as well; your current code isn't really caching anything, except a bit of not too demanding Twig code). You should probably move the second craft.entries query, the {% paginate %} tag and the {% include '_includes/partials/pagination' %} statement inside the {% cache %} tag pair.

Also, you might want to re-think having a default cache duration of 1 year – that's a long time, and if your site builds up a lot of pages, it could balloon your database and possibly cause issues with stalled Deleting state template caches tasks. My advice would be to keep the duration to something sane like 1 month, max – there's little point in keeping year-old caches around, normally :)

  • Thanks for this Mats. Some very good and valid points. I have updated to a month cache and moved the code around to benefit from the caching and breaking of those items. Commented Jul 20, 2018 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.