fix: make react preamble inline script async#472
fix: make react preamble inline script async#472hi-ogawa wants to merge 4 commits intovitejs:mainfrom
Conversation
|
I don't know the specs for async scripts, but what you are syaing is that this piece of code is guaranteed to be safe: <head>
<script async>window.foo = { bar: 1 }</script>
</head>
<body>
<script>console.log(window.foo.bar)</script>
</body>Because from my understanding we need the preamble to always init some global variables before any code app. |
|
Based on my quick testing, However, it's still possible that two |
|
Based on feedback on discord (mostly none 🥲), I think we don't have to bother with this. I'll go with recommending |
|
FYI it seems waku patches |
|
Good find. That's at least one users that this PR would help. 🙂 There should be no problem landing this PR, but in general, I think this is mostly a matter of how much we advocate one opinion over others while we don't make much opinion on SSR app in the first place. Advanced users / frameworks authors figure out their ways to do achieve the same anyways. For example, not just |
|
I guess there's nothing wrong with supporting two approaches, so I think we can land this. Let me re-open the PR. |
sapphi-red
left a comment
There was a problem hiding this comment.
I think it doesn't have any downsides and good to have this.
|
@dai-shi Just let you know react plugins will add |
|
Nice. |
|
I just read this section of mdn on async scripts and it says the order is not guaranty. Maybe this is fine in practice because every "React" script will also wait for "/@react-refresh" to be loaded, and this will be strange that scripts body get executed before head one then. I will turn this on and work for a few days with it to see if there are race conditions or not. |
|
I wonder if there's pkg.pr.new package for this PR? |
commit: |
|
Any ETA? |
|
This change won't be needed if we switch to |
Description
asyncoption for streaming #352(My review there was very off btw... 😢)
React HMR preamble script should be async so that it can be executed while ssr streaming. For Vite SPA, main client script is normally injected without async, so they are blocked until full html and it doesn't matter, but React SSR's
bootstrapModulesinjects<script type="module" async src="..." />inside body in a way that it's executed while streaming and client hydrates as early as possible.Though as mentioned in #471, utilizing
server.transformIndexHtmlto inject React HMR preamble for SSR streaming is somewhat hassle, so I chose not to do this in my simple demo in #471. But, some frameworks do this and making this async by default seems to be a nice-to-have improvement without downside.I tested the same change in my plugin hi-ogawa/vite-plugins#792.