Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.12+ / jQuery 3 - Entwine onmatch events firing before document-ready #1469

Closed
kinglozzer opened this issue Mar 14, 2023 · 2 comments
Closed

Comments

@kinglozzer
Copy link
Member

kinglozzer commented Mar 14, 2023

Still investigating this and trying to nailing down some easier recreation steps, as this is all a follow-on from tractorcow-farm/silverstripe-fluent#780. Currently I’m testing/debugging this by just hacking around in dist files, so to illustrate it you can dump the following in the dist file admin/thirdparty/popper/popper.min.js:

window.jQuery(() => console.log('domready'));
window.jQuery('body').entwine({
  onmatch: () => console.log('onmatch', window.ss.config)
});

In 4.12, onmatch is fired before document ready, prior to 4.12 it’s fired afterwards.

Digging deeper, the EntwineSubtreeMaybeChanged event is now firing prior to DOM-ready. The root cause of that is probably this change in jQuery 3 https://jquery.com/upgrade-guide/3.0/#breaking-change-document-ready-handlers-are-now-asynchronous

One workaround that appears to fix this is to wrap this runSoon() function so it’ll only fire after document-ready:

/** What to call to run a function 'soon'. Normally setTimeout, but for syncronous mode we override so soon === now */
var runSoon = window.setTimeout;

Instead use:

var runSoon = (fn, delay) => $(() => window.setTimeout(fn, delay));

PRs

@kinglozzer
Copy link
Member Author

kinglozzer commented Mar 15, 2023

I’m actually noticing this in SS5 too: currently TinyMCE appears to be attempting to initialise too early, causing console errors:

vendor.js?m=1678894496:2996 Uncaught exception ReferenceError: tinymce is not defined
    at Object.create (bundle.js?m=1675732377:1:221238)
    at Object.init (bundle.js?m=1675732377:1:220176)
    at e.fn.init.onadd (bundle.js?m=1675732377:1:223351)
    at o (vendor.js?m=1678894496:3054:64)
    at e.fn.init.onadd (vendor.js?m=1678894496:3398:58)
    at e.fn.init.onmatch (bundle.js?m=1675732377:1:223430)
    at o.<computed> [as onmatchproxy] (vendor.js?m=1678894496:3349:23)
    at HTMLDocument.<anonymous> (vendor.js?m=1678894496:3385:624)
    at HTMLDocument.dispatch (vendor.js?m=1678894496:25374:392)
    at m.handle (vendor.js?m=1678894496:25349:86) in onmatch on 

TinyMCE will still load (presumably onmatch fires again later), but applying the hotfix above stops the errors occurring.

Edit: this warning also shows in SS4

@GuySartorelli
Copy link
Member

PR merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants