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

DOMContentLoaded Listener executes after DOM is already loaded, widget doesn't mount #79

Closed
martingouy opened this issue Nov 11, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@martingouy
Copy link
Contributor

Hello!

In our production environment, this listener executes after the DOM is already mounted, and thus, the widget doesn't mount:
https://github.com/bhch/django-jsonform/blob/bf968559d15e1e79e9be71fb121fec62dee8bdd1/django_jsonform/static/django_jsonform/index.js#LL2C35-L2C35

  • We had to override it with window.addEventListener('load'... to get it working
  • The problem didn't happen on local

Two questions:

  • Are other people able to reproduce?
  • DOMContentLoaded was added in this commit: eaf03c7 . Any objection to changing it to load ? Happy to open a PR
@bhch
Copy link
Owner

bhch commented Nov 12, 2022

  • DOMContentLoaded: Fires after external scripts have been loaded and the DOM has been parsed. Doesn't wait for async scripts.
  • load: Fires after images, stylesheets, async scripts have been loaded.

Since we don't care about stylesheets and images, we want to render the widget as soon as the dependency scripts are available. So, we use the DOMContentLoaded event.

Only reason I can think of for it to not work is if the widget's script (index.js) is loaded as async.

@martingouy
Copy link
Contributor Author

@bhch Thanks for your answer.
index.js is not loaded async in our case.

The weird thing is that listening to the DOMContentLoaded event on the document object vs window object does the trick. Not sure exactly why, but I'll keep digging

@bhch
Copy link
Owner

bhch commented Nov 14, 2022

That's a great find! I just read some more about it and, indeed, document is the primary target of DOMContentLoaded event. So, it initially fires on the document and then bubbles up to the window object.

It's hard to tell what's preventing it from bubbling in your case (possibly some other script is using event.stopPropagation()), but if it works on document, then that's what we'll use. You can open a PR if you wish.

@martingouy
Copy link
Contributor Author

@bhch Sounds good. Just submitted a PR. Thank you for your work on this library.

bhch pushed a commit that referenced this issue Nov 15, 2022
Prevent widget initialization issues in case another script prevents the event from bubbling to the window object.

Issue: #79

Co-authored-by: Martin Gouy <martin@heyartifact.com>
@bhch
Copy link
Owner

bhch commented Nov 15, 2022

Fixed in #80. I will release a new version in a couple of days.

@bhch bhch closed this as completed Nov 15, 2022
@bhch
Copy link
Owner

bhch commented Nov 21, 2022

Released the fix in v2.15.0. Please upgrade. Thank you for the bug report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants