-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Fire synthetic load
events for hydrated elements
#11046
Comments
yeah, good point — would be inconsistent if we didn't |
I'm worried about the necessary runtime code that this would force on everyone getting out of hand - and I'm also worried that it will never be perfect, and we'll have a constantly moving target. IIRC, at least for the I'd prefer, rather than firing synthetic load/error events on every element regardless of whether the user needs them, that we provide actions (or whatever later 5.x thing that replaces actions) that abstract this away as best as possible and call a user callback either immediately or once the item has loaded/errored - without the additional overhead of runtime code for people who don't need it, and without the additional overhead of creating synthetic events for every element. |
Being unable to distinguish images that errored vs images that loaded but have a width and height of 0 does feel like a showstopper though. Absolutely farcical if there's truly no way to detect this. |
The non-bubbling-ness of these events does make me less nervous about constructing these as synthetic events. I'm not sure whether it makes me all the way not nervous about that or not. Maybe. |
This is a problem that is older than jQuery, I wonder if this is something that Svelte should try to solve. Maybe this should not be solved by Svelte, but for example by SvelteKit. One problem is that in Svelte 4 and below it was possible to work around this with a global function and using the I had two idea's neither are really great:
|
Ooh, yeah that's an interesting idea. If we only cared about event handlers added via attributes (or spread attributes) we could do this: <!-- input -->
<img alt="..." src="..." onload={foo} onerror={bar}> <!-- server output -->
<img alt="..." src="..." onload="this.__load = event" onerror="this.__error = event"> // client output
if (img.__load) {
foo(img.__load);
} else {
$.event('load', img, foo, false);
}
if (img.__error) {
bar(img.__error);
} else {
$.event('error', img, bar, false);
} (Mutatis mutandis for spread attributes.) Where it gets trickier (or much simpler depending on your perspective) is actions. Say you have an action that fades an image in on load: <img alt="..." src="..." use:smoothload>
<!-- server output -->
<img alt="..." src="..." onload="this.__e = event" onerror="this.__e = event"> // after the image is mounted (and actions have been attached)
if (hydrating && img.__e) {
img.dispatchEvent(img.__e);
} We could add that for every |
I would not refire the events for actions. But caching the events in |
Describe the problem
See #8301 and sveltejs/kit#11933 — if you have an
onload
handler (for an image or a link or an iframe or script or whatever) it will not fire (at least, not reliably) for elements that are being hydrated.Describe the proposed solution
This causes sufficient confusion and inconvenience that I have to wonder if we shouldn't just dispatch synthetic
load
events during hydration, if the element is already loaded.An alternative possibility is a
bind:loaded
directive, though this would force people to use effects to do work on load, which isn't ideal.Importance
nice to have
The text was updated successfully, but these errors were encountered: