-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Dependencies optimization in ssr Environments during dev request handling can cause inconsistent states #19323
Comments
I think this is the problem that was mentioned in #18358 (comment). I think we need
|
ah yeah definitely, I completely forgot about that comment! yes I think that's exactly the same thing
We (or better @jamesopstad) already explored this sort of thing1, James has put in place some hacking to basically revert the request handling execution when the issue happens, this is however quite complex and brittle because although you can re-run the request that can still cause various side effects like logs2 and state changes (imagine for example a request that when handled changes the state of a local DB 😖). So I think that something like this would be very unpractical 😓
Isn't that already what happens? a vite/packages/vite/src/node/optimizer/optimizer.ts Lines 517 to 520 in 353c467
Footnotes
|
Since the new dep discovery only happens when executing a dynamic import (IIUC), would it help if the The other way I can think of is:
Yes. I mean, I think we need docs and some tests that covers the feature as I guess no frameworks will listen the event in the current state. |
unfortunately I don't think so, because the issue is the amount of logic that gets run before the dynamic import is even encountered (let's say my dynamic import is in the middle of a side-effectfull function, the tricky part here is that the import is only encountered when half of the function has executed right?)
The difference is that there is no "real" error here (not one that the user should be aware of at least 😕)
yeah... although this feels pretty daunting... I feel like it might be best not to open such a can of worms 😓
Yes, we've also been considering that, but in my opinion it is better if we can still rely on ssr predundling as that really solves a lot of headaches on our side
This actually made me think, maybe we do need to explore this angle more and make sure that the whole app (server environment + client environment) is reloaded, this would break HMR but only for this single instance (which is relatively rare during development) |
Yeah, that's true.
The other ways still using the optimizer would be to aggressively bundle dependencies instead of not bundling it until discovered.
The alternative would be to change the chunking strategy of the optimizer. The reason why the reload is needed is to load the changed chunk when a dependency was added. If each dependency (including transitive ones) is in a separate chunk, we don't need to reload. This would create many chunks and is not suitable for browsers, but could be an option for non-browser environments. Although I'm not sure if that's possible in a performant way and if it causes problems with circular references. |
Describe the bug
The problem consists in the fact that during local development dependency optimization (
optimizeDeps
) can happen, in an ssr environment, while a request is being processed.This can be problematic as this allows modules to be reloaded while code is still being evaluated/run, meaning that the two can get out of sync.
Consider this very simplified example of such a scenario:
A
is encountered and optimizedB
is encountered (lazily)fullReload()
occurs (which invalidates the environmen's whole module graph)A
+ the environment state with the assumption that the two matchThe last point is where the issue occurs, the environment's state was modified, then the dependency
A
was reloaded and now the two might not match anymore, and this can be problematic for application.The minimal reproduction linked in this issue should explain things further, it also adds some details and context as to why this is something that can become a common issue for many consumers/authors of ssr environments.
Reproduction
https://github.com/flarelabs-net/vite-dev-ssr-prebundling-mid-flight-issue-repro
Steps to reproduce
No response
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: