Page Cache does not respect 'max-age' = 0

Created on 2 June 2015, over 9 years ago
Updated 17 July 2023, over 1 year ago

I tried setting headers:dnt and also max-age, but it has no effect. The #attached code is always cached.

Example snippet (change the text to see that the cache is not refreshing):

function mymodule_page_attachments(array &$page) {

  $dnt = (!empty($_SERVER['HTTP_DNT'])) ? [] : ['headers:dnt'];
  $page['#cache']['contexts'] = Cache::mergeContexts(isset($page['#cache']['contexts']) ? $page['#cache']['contexts'] : [], $dnt);
  $page['#cache']['tags'] = Cache::mergeTags(isset($page['#cache']['tags']) ? $page['#cache']['tags'] : [], $config->getCacheTags());
  $page['#cache']['max-age'] = 0;

  $script = 'alert('This is a test.');';
  $page['#attached']['html_head'][] = [
    [
      '#tag' => 'script',
      '#value' => $script,
    ],
    'mymodule_script',
  ];
}

How can I make the script dependend on a lot of rules? Best would be no cache at all.

πŸ› Bug report
Status

Closed: duplicate

Version

8.5 ⚰️

Component
Page cacheΒ  β†’

Last updated 3 months ago

Created by

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024