1

I am running into an odd issue where creating new child entries does not bust the cache of the parent page(s) that lists them.

This is the structure I am working with: enter image description here

I am displaying the Course entries on the Course Landing Pages like so:

{% cache using key craft.request.path %}

  {% set currentPage = craft.entries.id(entry.id).first() %}

  {% set entries = craft.entries({
    type: 'course',
    descendantOf: currentPage,
    with: [
      ['courseThumbnail', {
        withTransforms: [
          { mode: 'crop', width: 800, height: 800, position: 'center-center', format: 'jpg' }
        ]
      }]
    ]
  }) %}

  {% for entry in entries %}
      {% include '_includes/_listing-item-course' with { featured : true } %}
  {% endfor %}

{% endcache %}

When I publish a new Course entry, the cache for the parent page(s) is not busted and the new entry will not show unless I move/drag into a new position. This seems to clear the cache and the new entry shows on the parent page(s).enter image description here

If I change the craft.entries call to not include the descendantOf part, so it looks like this:

 {% set entries = craft.entries({
    type: 'course',
    with: [
      ['courseThumbnail', {
        withTransforms: [
          { mode: 'crop', width: 800, height: 800, position: 'center-center', format: 'jpg' }
        ]
      }]
    ]
  }) %}

Then newly added Course entries will show up straight away on all the Course Landing Pages.

Why is that?

  • There is definitely something going on with the descendantOf parameter. If I set entries like this {% set entries = craft.entries.type('course') %}, new ones show up straight away when published. The moment I change it to this {% set entries = craft.entries.type('course').descendantOf(entry) %}, they won't show up unless I manually clear the cache or move the new entry’s position in the structure tree. Very odd. – Katrin Jun 10 '17 at 12:46
2

This is a known issue – automatic cache busting currently doesn’t work when the descendantOf param is set to an actual element.

Unfortunately there’s no easy fix on Craft’s end, without a pretty drastic change to how cache busting works.

As a workaround, you can change your descendantOf param value to currentPage.id rather than just currentPage.

  • Thank you Brandon. I'm glad this is a known issue. However, using entry.id does not work for me either running version 2.6.2982. Any other workaround suggestions? – Katrin Jun 10 '17 at 14:23
  • I added my workaround solution as an answer. If you/anyone knows of a more efficient/elegant way, do let me know :) – Katrin Jun 10 '17 at 16:10
  • This also seems to apply when using entry.descendants or entry.getDescendants()? – Tom De Smet Jul 3 '17 at 13:49
  • And when using children()? – Tom De Smet Jul 3 '17 at 13:58
  • @TomDeSmet same – Brandon Kelly Jul 4 '17 at 13:38
1

Since using descendantOf(currentPage.id), as Brandon suggested, didn't work, I have come up with this workaround solution:

{% set currentPageID = entry.id %}

{% set entries = craft.entries.type('course') %}

{# only display entries that have currentPage as an ancestor #}
{% for entry in entries if currentPageID in entry.getAncestors().ids() %}
  {% include '_includes/_listing-item-course' with { featured : true } %}
{% endfor %}
1

I found a better solution. I'm using the CacheFlag plugin which solves this problem. And it's very easy to add to existing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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