I would like to let pages continue to benefit from Drupal 8 caching mechanism for anonymous users, except for a specific blocks which should update on every page refresh knowing that they are generated from Views.

Is it technically possible? How?

  • As MPD noted, use the max-age setting. Also you can do this via UI on block edit form too.
    – user21641
    Sep 25, 2015 at 8:35
  • I have this issue too - it is annoying Drupal doesn't deal with this automatically. Caching is good, but Drupal is supposed to be for dynamic websites...
    – James
    Jan 2, 2016 at 9:09

2 Answers 2


What does that views block show?

D8 has cache tags and contexts that will automatically invalidate the block if something changes.

So if you have a list of nodes, every time a node is added or updated, that block will be updated, but until then, the cached version is used. Which is much better than rebuilding on every page.

It also works with the page cache, those pages will be updated automatically as well.

Many, many things just work with caching out of the box, without the need for any manual configurations.

Edit (responding to comment):

Random with the random sort plugin? That should automatically mark that block as non-cacheable. That however does not affect the page cache module. There is now a new smartcache/dynamic page cache module but it will only be in the next beta (or RC!). That will eventually allow to cache the rest of the page and just build that part.

But a case like this is probably best implemented by using some javascript. If there only a few nodes you could display them all, hidden by default and just randomly make one of them visible. Or request a URL with javascript that returns a random Ad or even better visit a random of N URL's that each returns an ad (e.g. by returning a list of URL's and then picking one of them. You could even do that directly for the image URL.

  • The views returns a random node in order to vary the ads bloc.
    – Amine
    Sep 27, 2015 at 17:04
  • I will wait for the next release for the clean solution so :)
    – Amine
    Sep 28, 2015 at 18:45
  • Yes, the random is from the Views Random Sort plugin. I agree, it's possible to generate a javascript block content which put in a list the entities and then decide at browser layer with some random or circular function to pick and ajax one of them. However, I will have more than one block like this in some pages and a server side solution will fit best.
    – Amine
    Sep 28, 2015 at 18:55

If this is a custom block, then you can use the Cache API, and set the cache max age to 0, eg

$build = [
 // ...
 '#cache' => [
   'max-age' => 0,

If it is an existing block, then you can alter it to add this into the render array.

  • As explained in the question, my block is a Views block. I think it's too complicated to interfere with at php/module layer.
    – Amine
    Sep 28, 2015 at 18:44
  • 1
    Unfortunately, this doesn't work anyway (tried it). Seems like the page is delivered from pagecache and contains the outdated block. But I am still investigating. Jul 19, 2016 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.