All articles

Why a Drupal Views upcoming events list keeps showing past events

A Drupal View that lists upcoming events goes stale because its cache is invalidated by cache tags, and cache tags only fire when content is saved. Time passing saves nothing, so an event that finished yesterday stays in the list until someone happens to edit an event. The fix is to filter on the event's end date against now, switch the View to Views' time-based cache with a lifespan of a few minutes, and make sure every cache in front of it (page cache, CDN, a decoupled front end) also expires on the clock.

The situation

On a decoupled Drupal site, an events View fed an "upcoming events" block through GraphQL to a Next.js front end. Events that had already finished were still being listed. The filter looked right: event start date greater than or equal to today.

The View used the default caching setting, which is tag-based. Its stored cache metadata had a max-age of -1, meaning "permanent until a tag is invalidated".

Why tag-based caching never expires a date filter

Tag-based caching is the right default for most Views. The result set depends on content, so the View carries tags such as node_list (and, on recent Drupal versions, the bundle-specific node_list:event), and saving any event node invalidates them. A new event appears immediately; an unpublished one disappears immediately.

A relative date filter breaks the assumption behind that. The SQL that Views runs contains a concrete timestamp, worked out from today or now at the moment the query executes. Once the result is cached, that timestamp is frozen inside it. The data that decides what should be listed has not changed; the clock has. No entity is saved, no tag is invalidated, and with a permanent max-age nothing else ever clears it.

On a busy site the problem hides itself, because someone edits an event every day or two and the list refreshes as a side effect. It shows up on quieter sites, or over a weekend.

Filter on the end date, against now

The filter had a second, smaller problem. Filtering on start date >= today treats a three-day conference as "past" from its second day, while it is still running, and keeps an event that ended at ten this morning listed until midnight.

What "upcoming" usually means to an editor is "not over yet", which is end date >= now. That keeps events in progress and drops each one as soon as it ends. If the end date is optional on your event type, decide what an event without one should do: either make the field required, or add an OR group that falls back to the start date when the end date is empty. Otherwise those events silently vanish from the list.

Give the View a time-based cache

Views ships a time-based cache plugin for exactly this. In the View's Advanced settings, set Caching to "Time-based" and choose a lifespan for both the query results and the rendered output. Exported to config it looks like this:

cache:
  type: time
  options:
    results_lifespan: 300
    results_lifespan_custom: 0
    output_lifespan: 300
    output_lifespan_custom: 0

Five minutes was a sensible trade-off here: an event drops off at most five minutes after it ends, and the query runs at most a dozen times an hour per display. Pick the lifespan from how precise the list needs to be, not from how expensive the query is; for a list of upcoming events, minutes are fine and seconds rarely matter.

When the View is saved, its displays' cache metadata picks up a max-age of 300 instead of -1, and that is what lets the expiry travel outward. Render caching bubbles max-age up, so a block or a GraphQL response built from this View inherits the shorter lifetime.

The time-based plugin replaces tag invalidation for the View's own cache, so a newly published event can take up to the lifespan to appear. If editors expect new events to show instantly, the outer layers can still use tags for that, as below.

Every cache in front of the View needs to agree

Fixing the View is not enough if something in front of it caches the page forever.

  • Drupal's Internal Page Cache ignores max-age for anonymous pages; it relies on tags alone. If anonymous HTML is served straight from Drupal, a time-limited block inside a fully cached page will not expire on its own. Dynamic Page Cache and a reverse proxy that honours Cache-Control do respect it.
  • A CDN needs a Cache-Control header that reflects the shortest lifetime on the page, or its own short TTL for pages that list events.
  • A decoupled front end has its own cache, with its own idea of when a page is stale.

The Next.js front ends I work on use tag revalidation: when Drupal content changes, Drupal tells Next.js which tags to revalidate. That has the same blind spot as the View, because nothing tells Next.js that the clock moved. The fix there was the same idea: pages that render the events list get a short cache life, and they also carry the event list tag so a newly published event revalidates them straight away. With Next.js 16 cache components that looks like this:

import { cacheLife, cacheTag } from "next/cache";

export async function getLandingPage(path: string) {
  "use cache";
  cacheTag(`path:${path}`, "node_list:event");
  // Event lists change with the clock, not only when content is saved.
  cacheLife({ stale: 60, revalidate: 300, expire: 3600 });

  return fetchPageFromDrupal(path);
}

Only pages that actually list events need the short life. Pages of ordinary content can keep living until a tag invalidates them, which is the whole point of tag-based caching.

A rule for any date-driven list

Events are the common case, but the same failure applies to anything filtered by a relative date: "open" job listings with a closing date, "current" consultations, "this week's" news, scheduled publishing windows. Whenever a View's filter mentions now, today or an offset, ask what expires the cache when the clock passes the boundary. If the answer is "a content save", nothing does.

Key takeaways

  • Drupal cache tags are invalidated by saving content, so a View filtered by a relative date stays stale when only time has passed.
  • Use Views' time-based cache, with a lifespan of a few minutes, on any display whose results depend on the current date or time.
  • Filter upcoming events on end date greater than or equal to now, so events in progress stay listed and finished ones drop off promptly.
  • Check every cache in front of the View, including Drupal's Internal Page Cache, the CDN and any decoupled front end, because each needs its own time-based expiry.
  • Keep tag invalidation alongside the timer where editors expect new content to appear immediately.