The use case seem pretty common for example on event website. You want your old event not to appear on the "current event" listing when their date is in the past.

You will configure a filter like that:
filter config

The problem is that anonymous user still see the old events because the caches are note invalidate.

A workaround is to clear custom tag-cache on cron run https://bkosborne.com/blog/keeping-view-upcoming-events-fresh-drupal-8

Comments

gagarine created an issue. See original summary.

trichers’s picture

I have the same problem in all Drupal 8 Sites we´ve made. Is there a chance to get es solution in the core?

gagarine’s picture

Their is a module that try to fix this issue https://www.drupal.org/project/cache_control_override

Then you can set a time-based cache in your views and it will works!

Of course that should be fixed in core..

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

luksak’s picture

I have been using max-age extracted form the views results. It is quite cumbersome, but it works. You can either use the preprocessor of the view or an entity view mode.

It would be great to have this in core, but i can't imagine how complex a generic solution for this would be. A few thoughts:

We could use the offset set in each date filter and calculate the timestamps for each row when it wouldn't be included in the results anymore.
We can the use the minimum timestamp we get per row as the max-age

Maybe that should rather live in contrib? It serves the particular use case of an event calendar. What do others think about this?

But this is even more complex: How do we handle rows that are currently not in the results, but will be in the future? (Eg. a calendar that only shows events in the current month)

So for that use case a max-age on the view would make more sense and core can already do that.

And then there are daterange fields :)

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

abramm’s picture

StatusFileSize
new696 bytes

Just faced the same issue and did a quick research.
As mentioned by Lukas von Blarer, there's no reliable way of determining max-age automatically, especially if the minimum/maximum value is not in current results.

The safest option for now is to set max-age value 0 if the offset-based filtering is used; contrib or custom modules may override this behavior if it's possible to calculate max-age for a specific case (e.g. in case of events calendar).

Here's a simple patch implementing this.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

darvanen’s picture

I thought this was a good idea from #10:

We could use the offset set in each date filter and calculate the timestamps for each row when it wouldn't be included in the results anymore.
We can the use the minimum timestamp we get per row as the max-age

However, according to the helpful folks on Drupal Slack... views cache metadata isn't compiled at runtime but saved in config, so unless that changes this approach won't work.

I don't think disabling cache entirely is the answer though.

darvanen’s picture

Here's a start on a contrib module: https://www.drupal.org/project/vcp4dates

Feedback very welcome.


Edit: A friend asked me why do it in a contrib module instead of a patch, my answer:

Because it's quite complex and will likely need to go through heaps of iterations and multiple bugfixes which would be better served by it having its own entire issue queue, and since it's a plugin it can be a separate module. Also it was suggested in #10.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

darvanen’s picture

Just released first beta of https://www.drupal.org/project/vcp4dates

I finally figured out how to support multiple filters and archive views.

berdir’s picture

The most recent release of https://www.drupal.org/project/views_custom_cache_tag also provides limited support for this, but it's manual, not automated. Essentially, it's a combination of tag and time based cache invalidation including support for strtotime(). We commonly set our event views to have current day granularity, so events of the current day still show up as upcoming events for the whole day.

Now you can use views_custom_cache_tag with a custom expiration "tomorrow midnight" (so the calculated expiration will be seconds until midnight) *and* also use a node type specific cache tag for example.