head icon indicating copy to clipboard operation
head copied to clipboard

feat: events support on creating elements

Open ms-fadaei opened this issue 3 years ago • 16 comments

Resolve #46

ms-fadaei avatar Dec 11 '21 07:12 ms-fadaei

Big Question: how we can add event listeners to server-side rendered scripts on hydration?

ms-fadaei avatar Dec 11 '21 09:12 ms-fadaei

should continue to make a normal attribute if the value is a string so onload: "console.log('loaded')" will still work

egoist avatar Dec 11 '21 09:12 egoist

Big Question: how we can add event listeners to server-side rendered scripts on hydration?

there's no need to server-side render the <Script /> component I guess

egoist avatar Dec 11 '21 09:12 egoist

there's no need to server-side render the <Script /> component I guess

Sometimes we need our script load as soon as possible. This strategy should be used for any critical scripts that need to be fetched and executed before the page is interactive.

ms-fadaei avatar Dec 11 '21 10:12 ms-fadaei

should continue to make a normal attribute if the value is a string so onload: "console.log('loaded')" will still work

Yes of course. But closure will not work!

ms-fadaei avatar Dec 11 '21 10:12 ms-fadaei

well it seems next.js just ignores onLoad prop when the strategy is beforeInteractive, which is also ssr-ed, otherwise that script is created on the client-side only

egoist avatar Dec 11 '21 10:12 egoist

well it seems next.js just ignores onLoad prop when the strategy is beforeInteractive, which is also ssr-ed, otherwise that script is created on the client-side only

I didn't test this on real example but in the docs, it mentioned we can use onLoad

ms-fadaei avatar Dec 11 '21 10:12 ms-fadaei

https://stackblitz.com/edit/nextjs-wt4j8y?file=pages%2Findex.js maybe a bug then

egoist avatar Dec 11 '21 10:12 egoist

What I worrying about is maybe the event won't be triggered because the script is already loaded if (this is a big if) we add listeners on hydration

ms-fadaei avatar Dec 11 '21 10:12 ms-fadaei

the listener won't be triggered on hydration because we won't recreate the element to avoid loading it twice:

https://github.com/vueuse/head/blob/5ef9b6225e6c70e18011c33fb77d67de11af0d26/src/index.ts#L198-L201

but maybe we call newEl.onload manually if oldEl.onload is undefined

egoist avatar Dec 11 '21 10:12 egoist

the listener won't be triggered on hydration because we won't recreate the element to avoid loading it twice:

I looking for a way to attach the event to the existing element. But if find a way to do that, we can't be sure it will be called!!! I tried some ways already but they results in recreating.

ms-fadaei avatar Dec 11 '21 10:12 ms-fadaei

@egoist Hi again!

I tried a lot of ways and couldn't find a clean and straightforward way! I think a lot about it and what we did in Nuxt 2 and find out we really don't need to attach event listeners to the SSR script. They just load and evaluate as to their order of definition.

I also think the Next documentation has some mistakes. Maybe they wanted to mention afterInteractive and lazyOnload

ms-fadaei avatar Dec 11 '21 18:12 ms-fadaei

I will be very thankful if you merge this PR 🙏

ms-fadaei avatar Dec 13 '21 04:12 ms-fadaei

@egoist Please let me know can we merge this PR or not?

ms-fadaei avatar Jan 18 '22 07:01 ms-fadaei

Hey @egoist. I'll be looking forward to this feature. If everything is fine, please finish it.

davodaslanifakor avatar Jan 24 '22 06:01 davodaslanifakor

I've tested this locally with onload on script tags, everything seems to be working just fine It has been multiple months and it is a non-breaking change, can this be merged in @egoist @ms-fadaei ?

The only micro performance improvement would be to replace the regex by substring used in this PR https://jsbench.me/pzl6fl8rm3/1 Even though this wouldn't be noticable

mathieumagalhaes avatar Aug 04 '22 22:08 mathieumagalhaes

value would now be HTML encoded so any scripts would likely not work, otherwise this may introduce XSS issues for users

Could possibly implement this with https://github.com/vueuse/head/issues/95

Will close for now as the PR seems stale, but please comment on the above issue if you agree with the direction

harlan-zw avatar Sep 19 '22 05:09 harlan-zw