Search code examples
cachingtwigbrowser-cachedrupal-8

Drupal 8 strange caching issue


I have weird caching issue on Drupal site.

First D8 page caching is turned off.

Then I have some content on page that changes for every page loading - to make it simple I'm printing current time from twig template:

{{ "now"|date("H:i:s") }}

It works like this:

  • After clearing Drupal's cache and opening the page I can see current time, as expected
  • When I reload the page I still see previous time (I'm logged in as master admin). No matter how many times I reload the page I see time of the first page loading after clearing the cache
  • When I open the same page from another browser for the first time I see current time and after that for any reload I see time of the first page opening in that browser
  • When I inspect page loading from browser it says that page is loaded from server - not cached by browser
  • Clearing cookies doesn't help. For the first time I clear the cookie I get logged out and I see current time once, but for every other cookie clearing I see the same time.

Any idea what is happening here and how to disable that caching?


Solution

  • Add this to your themename.theme file

    function themename_preprocess(&$vars, $hook) {
      $vars['#cache']['max-age'] = 0;
    }
    

    and clear cache.

    Edit, on drupal.org you can read good guide how to prepare your development environment and disable cache during development. Guide