Hi there,
Problem/Motivation
When we add the attribute { async : true } in a js libraries, the script tag resulting in HTML source get the async attribute. That is very helpfull to optimize pageload speed when the library is not necessary for first render. When doing that, the custom behaviors are randomly executed due to the moment where domready is called, the browser, etc.
Firefox execute it some times, Chromium never, and so on.
I would like to write js behaviors capable to be ran on synchronous or asynchronous libs.
Proposed resolution
I propose to add an eventListener on async (and defer ?) script tags to execute Drupal.attachBehaviors() on asynchronous scripts too.
No sure what's the best place to do it, the patch proposes to insert it on drupal.init.js
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | core-3085197-24.patch | 854 bytes | mloyat |
| #21 | core-3085197-21.patch | 833 bytes | nod_ |
| #20 | 3085197-20.patch | 572 bytes | shivam-kumar |
| #18 | 3085197-18.patch | 548 bytes | _utsavsharma |
| #16 | interdiff_15-16.txt | 940 bytes | pradhumanjain2311 |
Comments
Comment #2
mloyat commentedComment #4
mloyat commentedAdapting to domready deprecation.
Comment #5
mloyat commentedComment #7
jamesashok commentedModifying existing patch -
Changes made -
Comment #13
nod_we don't have .es6.js files anymore.
Comment #14
akram khanCreated patch for 10.1.x
Comment #15
akram khanTrying to Fix CCF and drupal.init.es6.js is remove from 10.1.x
Oops! added wrong file
Comment #16
pradhumanjain2311 commentedTry to fix CCF.
Comment #17
nod_Here if there is no async script on the page, attachBehaviors is never run :o
Need to keep this call to not break everything.
Instead of going old school we can do something like
or something close to that.
I think just binding to window load would be enough as well if there are async script on the page?
Comment #18
_utsavsharma commentedMade changes as per the comment #17.
Comment #19
nod_Thanks but the changes are missing an important part of the patch (the listener function) and it's not keeping the original attachbehavior call.
Comment #20
shivam-kumar commentedHave tried to fix custom command fails and code fixes.
Comment #21
nod_Thanks, it seems the code was rerolled without fully getting the different pieces of the patch.
Clean reroll, it's a pretty impacting change so it really needs some tests to make sure we don't have a regressions since core doesn't use that feature itself (which could be a reason why not implementing that in core).
Comment #22
smustgrave commentedChange looks good to me. See the clean tests. Also see your comments about not needing tests here, unless i read that wrong.
Comment #23
nod_Wasn't clear it needs test otherwise it'll be really easy to break
Comment #24
mloyat commentedBy the way, the behavior attached to async scripts is called without context nor settings passed as argument.
Is that on purpose ? I think it should to be consistent with first call, but not sure of which context to pass as first argument....