- πΊπΈUnited States wesleymusgrove
Unless we have many developers not using advagg that ran into this issue I don't see core are the place to fix this
Since AdvAgg is no longer recommended for D10 (and causes issues like not saving newly generated bundles of JS to disk unless you uncheck "Enable advanced aggregation" π Document which parts of the module are still relevant after aggregation changes in 10.1.0 Needs review ), I've completely uninstalled it, but still needed a way to defer all JS as @nod_ mentioned in #42 π Drupal main javascript file can't be loaded with "defer" attribute after upgrade to 8.8.1 Fixed .
So I added this simple hook to `defer` all scripts:
<?php /** * Implements hook_js_alter(). */ function custom_js_alter(array &$javascript) { foreach ($javascript as &$js) { if (is_array($js) && isset($js['attributes'])) { $js['attributes']['defer'] = 'defer'; } } } ?>
Before uninstalling AdvAgg, I had previously applied the core patch from #45 β , which I re-rolled for D10.
After uninstalling AdvAgg and adding my `custom_js_alter` hook, I started seeing this issue happen again where Drupal behaviors weren't executing if they were attached after drupal.init.js had loaded, as would be the case for all the deferred scripts.
Since @nod_ mentioned that
it is safe to call multiple time[s]
on #38 π Drupal main javascript file can't be loaded with "defer" attribute after upgrade to 8.8.1 Fixed , would a custom module adding some JS like this to call `Drupal.attachBehaviors` one last time after the document is `complete` be an appropriate solution?
(function (Drupal, drupalSettings) { var domReady = function domReady(callback) { var readyStateCheckInterval = setInterval(function () { if (document.readyState === 'complete') { callback(); clearInterval(readyStateCheckInterval); } }, 100); }; domReady(function () { Drupal.attachBehaviors(document, drupalSettings); }); })(Drupal, window.drupalSettings);
If someone aggressively defer all scripts without using advagg I would hope they know what they are doing. the drupal.js/drupalsettings scripts are light enough to not have to defer them
I realize this technique _does_ aggressively defer all scripts and that is my goal. Is there a list of core drupal JS files that should be excluded from getting the `defer` attribute for D10 core's `Drupal.attachBehaviors` to continue working as expected even for deferred scripts that define new behaviors?
Thanks! I know this has been closed for a while, but it seemed relevant to this conversation.