Should HyperApp Wait For The DOM Before Rendering?
See original GitHub issueIf you only want to make your vote count please just 👍 or 👎 this comment. I added both so it’s easy to click.
Things that need to be done
- Decide if
window.addEventListener('load')
should be implicit - Add documentation for agreed upon behavior
The Problem
When including app()
in a script in <head>
hyperapp will crash because it tries to append itself onto the not-yet-existing DOM.
Note: Some might suggest to just include the script at the bottom of the page, however - in some use-cases (like mine) that’s not possible. It also feels like a workaround IMHO.
This can be solved in (at least) 2 ways:
Solution 1
Force users to add window.addEventListener('load', () => app(...))
to their scripts.
import {app, html} from 'hyperapp'
window.addEventListener('load', () => app({
view: html`<div>Example</div>`
}))
Pro
- less implicit behavior, more control/information for the dev
Contra
It just crashes for no reason, even though I copied the whole example! - Frustrated first-time users.
Solution 2
Implicitly do the same as in solution 1.
import {app, html} from 'hyperapp'
app({
view: html`<div>Example</div>`
})
Pro
- more straight-forward code
- less stuff to consider/know for the dev
It just works!
Contra
Might be barely considered “Magic”. Should be properly documented. However I don’t see any use-case where hyperapp is supposed to crash. 😄
Meta:
Follow-up issue
Initial issue: subs
didn’t work when ran by onload
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:14 (9 by maintainers)
Just to show how this fails when attempting to use a script in the
head
tag, check out this jsbin example. If you open up the console you’ll see that the code fails when the app attempts to create the default root element.I always attach scripts in the body, so I never see this issue, but #64 seems like a minimal change in order to support also including scripts in the head, which some users may do.
I also believe in the fail-fast mentality, so at the very least it might be worth noting in the docs that users should include scripts just before you need it, and no sooner, i.e. in the
body
.This is implemented since 0.3.0 or 0.4.0, can’t remember.