Skip to content

Instantly share code, notes, and snippets.

@terryupton
Created April 15, 2025 08:08
Show Gist options
  • Select an option

  • Save terryupton/cd2c1ffcaff54922e42ac9b53e8400c1 to your computer and use it in GitHub Desktop.

Select an option

Save terryupton/cd2c1ffcaff54922e42ac9b53e8400c1 to your computer and use it in GitHub Desktop.

Revisions

  1. terryupton created this gist Apr 15, 2025.
    22 changes: 22 additions & 0 deletions cache-bust-pending.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    {# Fetch the next **pending** entry due to go live #}
    {% set nextToCome = craft.entries()
    .section('articles')
    .status('pending')
    .orderBy('postDate asc')
    .one() %}

    {# If there is no entry due to go live, cache the fragment for a year (change to what you need) #}
    {% set cacheExpiry = nextToCome ? nextToCome.postDate : now|date_modify('+1 year') %}

    {% cache until cacheExpiry %}

    {# Query in your cache tags #}
    {% set items = craft.entries
    .section('articles')
    .orderBy('postDate asc')
    .all()
    %}

    {# Your loop... #}

    {% endcache %}