Comments (32)
@dodekeract Ah, what @tunnckoCore said. Good job on catching that!
from hyperapp.
@tunnckoCore Me neither, but I suspect @dodekeract is embedding the bundle.js in the <head>
section.
from hyperapp.
@jbucaran That's one way to put it, yes.
from hyperapp.
Subscriptions are functions that run only once when the DOM is ready.
I guess by the time the subscription was called, the window.load event had already fired.
Yes
app({
subscriptions: [
myFunction
]
})
No
app({
subscriptions: [
_ => window.addEventListener('load', myFunction)
]
})
from hyperapp.
@jbucaran My bad, I thought it's obvious that ...
is the whole hyperapp
stuff (() => app(...)
). Won't it crash when it can't find the DOM yet? React didn't like being called before the 'onload' event.
from hyperapp.
Which event should be used to start the code?
What mean "to start the code"? You don't need anything. App should be called before the dom-is-ready. If something in your app should happen when DOM-is-ready put it into subscriptions.
Won't it crash when it can't find the DOM yet?
No.
from hyperapp.
@tunnckoCore I just removed the event listener and it caused a crash, as usual.
Uncaught TypeError: Cannot read property 'appendChild' of null
at 108.module.exports (some-script…:104)
at Object.475 (some-script…:1184)
at __webpack_require__ (some-script…:20)
at some-script…:66
at some-script…:69
Crash points to document.body.appendChild
in hyperapp's source.
EDIT: Actually, now it's crashing only about 50% of the time. Sometimes it just works. Code doesn't have anything complicated whatsoever though. It's even less than the counter example.
Source
import {app, html} from 'hyperapp/hx'
app({
view: () => html`
<div style=${{
position: 'fixed',
bottom: '8px',
right: '8px',
backgroundColor: 'yellow'
}}>
View
</div>`
})
<html>
<head>
...
<script src="https://some-domain/some-path.js" async crossOrigin="anonymous"></script>
</head>
<body>
</body>
</html>
Transpiled by babel
and packed by webpack
.
from hyperapp.
@dodekeract Is the problem the <script async>?
from hyperapp.
@jbucaran No, it also appears without async
.
Actually, now that I tested that it always happens when no async
is used.
from hyperapp.
@dodekeract Can you break down the problem into a smaller problem?
First, try with this HTML
<!doctype html>
<html>
<body>
<script src="/bundle.js"></script>
</body>
</html>
from hyperapp.
@jbucaran Loads reliably without crashing. Problem is that I can't use that as the script will be used/embedded by third parties and I can't force them to include it at the bottom of the page.
So with the current code-base I wouldn't be able to use subs
at all, since they don't trigger after load
.
from hyperapp.
@dodekeract So, the problem may be in your other HTML, right?
from hyperapp.
from hyperapp.
@jbucaran I don't think so. I actually only moved the script
down, didn't change the src=
part or anything else.
Looks like a classic "body isn't available yet" issue to me. Especially since it sometimes (probably when not read from memory-cache) works when using async
.
from hyperapp.
@dodekeract How should we handle this without adding the <script> to the last line of the HTML?
Hmm. Can you post the entire code? Both your HTML and the script. You can use the details/summary tags, so it doesn't matter if it's long.
from hyperapp.
I'll have to create a completely minimal repro then. Current code has some stuff I can't share that's not related to hyperapp
.
from hyperapp.
@dodekeract I just want to see the location where you are embedding the bundle.js that contains both hyperapp and your application.
from hyperapp.
Still can't understand what we are talking about.
I'll have to create a completely minimal repro then.
👍
from hyperapp.
@jbucaran @tunnckoCore https://github.com/dodekeract/hyperapp-issue-61
npm run build && npm run start
index.html
and onload.html
work while broken.html
doesn't work.
And yes, I do embed it in the head, as specified in my initial summary/details
. It's not possible for me to force all users to embed it at the end of the website and I don't think it's a good solution to the problem anyway.
from hyperapp.
@dodekeract Thanks for creating a demo repo.
It seems you are embedding the bundle.js in the <head>
tag, when you shouldn't.
When you run app(...)
, HyperApp will try to call document.appendChild(vnode) somewhere during the patch process, but document
will be undefined, hence the error:
Cannot read property 'appendChild' of null
from hyperapp.
@jbucaran Which is why I'm asking which event I'm supposed to use, as load
seems to break the subs
. 😉
from hyperapp.
@dodekeract Yup, now I see that's what you were asking :)
Isn't the solution not embedding bundle.js inside <head>
?
from hyperapp.
@jbucaran not possible in my use-case and I think that's actually more of a workaround. Until now I always used window.addEventListener('load')
with an async
script.
from hyperapp.
First to mention that app()
does not return anything, so
window.addEventListener('load', app({
in index.onload.js
is wrong thing.
Second, set something in the body and try to add opts.root
.
from hyperapp.
@tunnckoCore Whoops, that's a typo, pushed the fix to the repo. However it actually did solve the crash. Don't ask me why though.
from hyperapp.
@jbucaran @tunnckoCore broken.root.html
is also broken.
from hyperapp.
@dodekeract Of course broken.root.html is broken. You are calling document.getElementById
inside <head>
.
from hyperapp.
@jbucaran I'm aware of that. I didn't expect it to work. As I said I can't expect the script to be at the end of the body in my use-case, as it's mainly embedded by third-parties.
from hyperapp.
@dodekeract So the problem is simply you need subscriptions and you're loading HyperApp via the <head>
tag?
from hyperapp.
Okay, gotcha. Let's track this in #62.
The fix involves adding a bit of code to app.js. We need to check whether the DOM has already loaded before adding the DOMContentLoaded listener to document.
@dodekeract Can you work on a PR?
from hyperapp.
@jbucaran Is there any reason why we can't just treat "render app()" as the first sub?
from hyperapp.
Locking as the entire discussion is now out-of-date.
from hyperapp.
Related Issues (20)
- A way to insert raw Html HOT 1
- TypeError: can't access property 0, newSubs is null, when setting the state to undefined. HOT 4
- Issue with null-vnodes HOT 1
- prevent rerender node HOT 2
- The dispatch initializer ends in an endless loop on init when dispatching any action HOT 7
- Injected classes gets removed when using object/array to define class props HOT 1
- hyperapp version HOT 3
- Memo Data Gotcha HOT 5
- Confusing doc for actions -> wrapped actions HOT 5
- Passing arguments to init HOT 4
- [Question] Headless mode is still possible? HOT 1
- Destroying a child app HOT 8
- @hyperapp/html: use a Proxy? HOT 9
- Actions returning other Actions HOT 5
- Compile template tag to hyperscript HOT 17
- A challenge to hyperapp community HOT 1
- Has 2.0 been dropped from development? HOT 3
- oldSub[2] is not a function HOT 3
- Cannot read properties of null (reading 'length') HOT 5
- Unlikely Use Case bug in HTML and SVG Packages HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hyperapp.