You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: Fixes and improvements for HMR and module graph (#597)
## Fix module graph for SSR modules
### 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
There was a bug in the way we parse the vite SSR imports - we weren't matching on cases where there were extra arguments being passed to these functions. As a result, vite wasn't able to construct the module graph correctly. Ultimately, this meant that when some modules were invalidated, their importers should have been invalidated too, but werent (since the link between them in the graph wasn't there).
## Invalidate explicitly for SSR
On changes to files in SSR graph, we now invalidate the relevant module and as well as its importers (recursively), then invalidate the same way for the corresponding virtual modules in the worker module graph, as well as its importers (also recursively).
## Better client module invalidation
We now:
* look at all client updates - rather than only ones that were also worker updates as we did previously; then
* invalid all the modules vite tells us where relevant when a file changes - previously we'd pass control on to vite's own HMR invalidation logic for use client modules and css files, and otherwise short circuit, but this turns out to be necessary
## Improvement: Avoid redundant logic for client/server lookups
We maintain "lookup" modules for modules with `use client` and `use server` directives, in order to find and import them on demand when RSC needs them. We were handling different variations on how these modules might be resolved (with/without `/@id/`, with/without `.js). We now instead avoid the possibility of there being multiple ways for these to be resolved, so that there's only one request id every specified for each - `virtual:use-client-lookup.js` and `virtual:use-server-lookup.js`) => we were able to remove complexity by only needing to resolve these modules 1 way instead of multiple different ways.
0 commit comments