craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

[4.x]: Pending entries do not seem to clear template cache when published #16038

Closed mikesnoeren closed 6 days ago

mikesnoeren commented 6 days ago

What happened?

Description

In our project, we have entries with a future post date, which are saved in a "pending" state until the post date passes. The issue is that the template cache doesn't clear when the entry is published (i.e., when the post date is reached), but only when the entry is saved initially (while in "pending" state).

Steps to reproduce

  1. Create an entry with the post date set to a future time. (Note: The cache is cleared when the entry is saved in the "pending" state.)
  2. Visit the entry overview page, where all entries are displayed. (Note: The cache for this page is now rebuilt.)
  3. Wait until the post date passes, and the entry status changes to "live".
  4. Visit the overview page again, and see that the newly published entry is not listed.

(Optional additional steps):

  1. Create another entry with the post date set to a near-future time. (Note: The cache is cleared when this entry is saved.)
  2. Visit the overview page again, and see the earlier published entry is now listed. (Note: The cache clears due to the previous step.)
  3. Wait until the post date passes, and the entry status changes to "live".
  4. Visit the overview page again, and see the newly published entry is still not listed.

Expected behavior

The cache should be cleared when the entry is published (status changes to "live"), not when it's saved in the "pending" state.

Actual behavior

The cache is cleared when the entry is saved (while in "pending" state), not when it is published.

simplified code example

{% set cache_key = 'articles' %}
{% set query = craft.entries.section('articles').orderBy('postDate desc') %}

{% cache using key cache_key %}
    {% paginate query.limit(12) as pageInfo, items %}

    {% for item in items %}
        {# code for the individual items #}
    {% endfor %}

    {{ include('_inc/helpers/paginate.twig', {
        pageInfo: pageInfo
    }) }}
{% endcache %}

Craft CMS version

Craft Pro 4.12.6.1

PHP version

8.2.24

Operating system and version

Linux 6.11.5

Database type and version

MySQL 5.7.42

Image driver and version

GD 8.2.24

Installed plugins and versions

Name                            Handle                 Package Name                            Version  Installed  Enabled
------------------------------  ---------------------  --------------------------------------  -------  ---------  -------
CKEditor                        ckeditor               craftcms/ckeditor                       3.9.0    Yes        Yes    
Dashboard Begone                dashboard-begone       putyourlightson/craft-dashboard-begone  2.0.0    Yes        Yes    
DigitalOcean Spaces Filesystem  dospaces               vaersaagod/dospaces                     2.0.0    Yes        Yes    
Feed Me                         feed-me                craftcms/feed-me                        5.8.1    Yes        Yes    
Freeform                        freeform               solspace/craft-freeform                 5.6.4    Yes        Yes    
GPT Content Generator           gpt-content-generator  soerenmeier/gpt-content-generator       1.2.0    Yes        Yes    
Hyper                           hyper                  verbb/hyper                             1.3.2    Yes        Yes    
Kint                            kint                   verbb/kint                              3.1.1    Yes        Yes    
MatrixMate                      matrixmate             vaersaagod/matrixmate                   2.2.1    Yes        Yes    
Navigation                      navigation             verbb/navigation                        2.1.1    Yes        Yes    
Retour                          retour                 nystudio107/craft-retour                4.1.19   Yes        Yes    
SEOmatic                        seomatic               nystudio107/craft-seomatic              4.1.4    Yes        Yes    
Super Table                     super-table            verbb/super-table                       3.0.15   Yes        Yes    
Vite                            vite                   nystudio107/craft-vite                  4.0.10   Yes        Yes 
i-just commented 6 days ago

Hi, thanks for getting in touch!

This is expected behaviour. As you said, the cache is invalidated when an entry is saved. If that entry happens to have a publish date that’s in the future, then it’ll show as having a “Pending” status and won’t be returned when you query for live entries. Then, when you query for the live entries again, and that post date turns out to be in the past, the entry is returned (and shows with a “Live” status), but no further saving is done to get to that place (nothing in the database changes).

There’s a StackExchange post that talks about how you can achieve what you’re after.

And then there’s this related discussion: https://github.com/craftcms/cms/discussions/9170.

I hope this helps!

I’ll close this now, but feel free to reply if you run into any further issues.