-
Notifications
You must be signed in to change notification settings - Fork 6
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
should keep script async attribute to false for block executing scripts #7
Comments
@kuitos curious what impact you are seeing from this behavior? Currently the blocking scripts are handled by writabledom itself without changing the underlying property (just uses attribute presence). Changing the property wouldn't really have an impact besides other code explicitly reading that async property? (specifically here it uses an attribute check instead of a property check https://github.com/marko-js/writable-dom/blob/main/src/index.ts#L161) |
Oh my bad your issue is with "defer" scripts. FWIW I recommend avoiding defer scripts and instead preferring actual asyn scripts (especially when streaming) |
Yeah this issue is all about defer scripts.
But in my knowledge, some popular frontend frameworks used defer to improve there first screen performance by default, especially in streaming ssr mode, like vue and nextjs. |
The problem with defer is that these scripts cannot execute until the entire page is done, but with async you have (in a non blocking way) the script execute as soon as possible. if the framework you're using supports async hydration then you should always use async over defer since the user will be able to interact with the page sooner. Either way ideally this module would support defer. It's a bit confusing as to way defer should do in a micro-frame though. Normally defer waits until DOMContentLoaded to execute, however I guess here it'd have to wait just until the writable dom instance has closed. One problematic piece here is that if you use |
Yes, that's what I was thinking. Another problem is how to block the execution of deferred scripts. My thought is to remove their
Sorry I am not familiar with |
If we don't consider fully consistent to browser behavior for now (defer scripts must execute before DOMContentLoaded but after HTML parsed), and only need to ensure that defer scripts execute in order, as long as they are inserted into the document with async set to false before execution, does this make sense? |
Version: 1.0.3
Details
I found that non-async scripts marked with defer will become async after insertion, which causes the defer scripts not to be executed in document order. There is no problem with non-defer synchronous scripts because we have a blocking flow that waits for the previous script to finish before loading the next one.
I did some digging and found that this is because scripts written through a documentFragment will all become async true, and the same issue exists with document.importNode.
Here is example code to explain the issue:
Expected Behavior
defer script without async attribute on html should execute in the order in which they appear in the document.
Actual Behavior
defer script without async attribute on html not executed in order
Possible Fix
set script element async attribute correctly before it been append to target document
writable-dom/src/index.ts
Line 121 in 020d50b
Your Environment
The text was updated successfully, but these errors were encountered: