Commit 76e9865
authored
fix: More HMR fixes for robustness (#598)
## Avoid js transforms for css
### Context
To support SSR having its own module resolution behaviour (e.g. not using `react-server` import condition or RSC react runtimes) while still existing in the same runtime env as the rest of the worker (where `react-sever` import condition and RSC react runtimes should apply), we have separate `worker` and `ssr` environments, then have an "ssr bridge" concept: in the `worker` environment, for all modules to be evaluated in SSR context, we fetch code from the `ssr` environment for that module.
We parse the code we fetch from the SSR environment to find all imports vite does (`__vite_ssr_import__`, `__vite_ssr_dynamic_import__`), and add `import`s for these fetches so that vite will build the module graph correctly.
### The fix
We do this fetching and transforming too for css modules too - except in these cases the result of the fetch is an empty string, and should remain that way rather than getting any other code added to it, in order to avoid adding js to the css.
## Opt out of custom HMR logic for non js and css files
If the js files are not js (e.g. js, jsx, mjs, cjs, ts, mts, etc) and not css, we should ignore them and pass control on to other plugins. We were trying to process .sqlite files during HMR, for e.g, which was resulting in HMR of these modules causing miniflare to crash.
## Skip HMR for SSR env when files are not in "use client" import chains
HMR for SSR should only look at "use client" files, or files imported by "use client" files. It was processing for other files (e.g. .sqlite files - while we avoid non js/css files now regardless, it still shows the need for more thorough checking for the use client graph for HMR in SSR env).
## Skip HMR for client env when files are not in "use client" import chains
It is possible for the client environment to contain files that are neither marked with "use client" nor imported by files that are. For example, if there is a style.css imported in worker module graph. For this specific example, to be honest, I'm not entirely sure why for this example case, I noticed this when inspecting the module graph. Regardless, it shows the need for more thorough checking for whether the module is in fact part of "use client" import chains.
For this reason, we short circuit HMR for the client env if we the file is not in a "use client" import chain.
## Avoid recursive invalidating for SSR modules
Turns out we only need to invalidate the SSR modules themselves - not their importers as well - for HMR to work correctly for SSR.1 parent f773e31 commit 76e9865
2 files changed
+68
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
48 | 82 | | |
49 | 83 | | |
50 | 84 | | |
| |||
73 | 107 | | |
74 | 108 | | |
75 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
76 | 114 | | |
77 | 115 | | |
78 | | - | |
79 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
80 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
81 | 127 | | |
82 | 128 | | |
83 | 129 | | |
84 | 130 | | |
85 | 131 | | |
86 | | - | |
87 | 132 | | |
88 | 133 | | |
89 | 134 | | |
| |||
152 | 197 | | |
153 | 198 | | |
154 | 199 | | |
155 | | - | |
156 | | - | |
157 | | - | |
| 200 | + | |
158 | 201 | | |
159 | 202 | | |
160 | 203 | | |
| |||
179 | 222 | | |
180 | 223 | | |
181 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
182 | 235 | | |
183 | 236 | | |
184 | 237 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
152 | 153 | | |
153 | 154 | | |
154 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
155 | 163 | | |
156 | 164 | | |
157 | 165 | | |
| |||
169 | 177 | | |
170 | 178 | | |
171 | 179 | | |
172 | | - | |
| 180 | + | |
173 | 181 | | |
174 | 182 | | |
175 | 183 | | |
| |||
0 commit comments