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.

Comments

hass’s picture

Issue summary: View changes
dawehner’s picture

Issue tags: -D8 Accelerate Dev Days

I would have expected that even does doesn't work.

 * If you try to add anything but #attached and #post_render_cache to the array
 * an exception is thrown.
 *
 * @param array &$attachments
 *   An array that you can add attachments to.
 *
 * @see hook_page_attachments_alter()
 */
function hook_page_attachments(array &$attachments) {
hass’s picture

Bedir has provided a patch to add the cache tags and this at least works. No idea why the rest does not. I need the rest for Google Analytics, too.

Berdir’s picture

No, you don't.

The anonymous page cache does not support cache contexts and doesn't respect max-age. By design.

Adding this would mean that you would completely break the page cache for the whole site. Obviously, that would be a very bad idea.

Again, the page cache is no different to Drupal 7, you had exactly the same problems there, you just didn't notice as it wasn't enabled in the tests.

Yes, smartcache is a different topic but that doesn't exist yet. That will respect contexts and it will work as long as you ensure that the relevant contexts are there. Which is a good reason to use condition plugins with context but a few things are still missing for that to properly work.

As I've told you multiple times and others told you as well in the smartcache issue. There is only one way to make the DNT header work when combined with page cache: You need to do it in JS.

hass’s picture

Fabian said I can use headers:dnt and it does not require JS.

dawehner’s picture

Well, if your module does not support page_cache module, you could communicate that otherwise.

Wim Leers’s picture

Title: Cache does not respect 'max-age' = 0 » Page Cache does not respect 'max-age' = 0
Priority: Major » Normal

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Component: request processing system » page_cache.module
Wim Leers’s picture

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bradjones1’s picture

dpi’s picture