Disable caching when snippet is present

I have a snippet which displays upcoming events. Past events disappear automatically when the current date has passed. It would be handy if I could disable the page cache then within the snippet. How could I achieve this?

That won’t work. By the time you reach the snippet, the page is already rendered from cache.

Is there a way to know when the snippet is used? Like loaded by some page field setting?

It actually is a block snippet. And the block element can be used on different templates. I could add a hidden field to this block. But how to check for it in the cache config ignore setting for example?

Within the cache.ignore config option, you can check if the page contains the given block type.

So something like

 'ignore' => function ($page) {
        return $page->text()->toBlocks()->has('blockTypeThatShouldPreventCaching');
      }

Ah yes, this is a great solution. Unfortunately I have nested blocks in my case. So sometimes the “uncacheable” block is on the second level, which makes it harder to identify. I now thought about a dirty workaround: just including $kirby->session() inside my snippet, which prevents the page from being cached. Maybe a core method like $page->disableCache() could be better for this. What do you think?

I mean in theory you should be able to do $kirby->response()->cache(false);

Thanks. Yes this works and seems to be the most flexible and comprehensible solution in my case.