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 ?

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().

  • 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 '18 at 15:03
  • then set the cache metadata in $variables['#cache'] – 4k4 Feb 21 '18 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 '18 at 15:35
  • 1
    #attached and #cache are both bubbleable metadata, if one of them is passed then the other is too. – 4k4 Feb 21 '18 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 '18 at 22:03
0

OK, the cache tags used for these settings was the wrong one so nothing was invalidate...

My understanding is you have to add cache metadata to on the "region" that is responsible for the drupalsettings building, so when it will be invalide it could update them, Drupal manages to detect that drupalsettings have been update on page rebuild (since all regions invalidations bubble up to page level).

Sorry for all the fuzz...

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.