4

I use drupalSettings to send config data to javascript. But when I edit my config, the javascript object is not modified, I have to clear all cache to get the update. The cache metadata in http headers show me that the page is linked to my 'config:my_module.settings' tag but the inline drupalSettings declaration is left unchanged.

What's the way to add cache metadata to:

$attachments['#attached']['drupalSettings']['example']['foo']
//or in hook_preprocess_hook ()
$variables['#attached']['drupalSettings']['example']['foo']

Or is there a function that could rebuild drupalSettings ?

1 Answer 1

6

When you add attachments you can add cache data as well:

$attachments['#cache']['tags'][] = 'config:my_module.settings';

BTW #attached and #cache are the only data you are allowed to add in hook_page_attachments() or hook_page_attachments_alter().

10
  • That's seems correct, but sorry I copy past the example form the official doc and forgot to point that I actually add the settings in a hook_preprocess_hook using $variables['#attached']['drupalSettings']...
    – Teenage
    Feb 21, 2018 at 15:03
  • then set the cache metadata in $variables['#cache']
    – 4uk4
    Feb 21, 2018 at 15:09
  • I don't think so, the cache metadata will be linked to the region and not passed to the #attache, no ?
    – Teenage
    Feb 21, 2018 at 15:35
  • 1
    #attached and #cache are both bubbleable metadata, if one of them is passed then the other is too.
    – 4uk4
    Feb 21, 2018 at 16:04
  • Yes, but bubbling only affects parents not siblings, the Drupalsettings "region" is in the bottom of the page and so is left "untouched", no ? If I follow you correctly the Drupalsettings should inherit the same metadata as the page, it doesn't seems to work for me... Anyway, I will try to use hook_page_attachememts to add the cache metadata
    – Teenage
    Feb 21, 2018 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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