-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix: resolvedUrls is null in plugin's configureServer after server restart #15450
Conversation
Run & review this pull request in StackBlitz Codeflow. |
f9e4cfe
to
291c0a5
Compare
After restarting the server, does |
Thanks for investigating this! The indirections in server handling today took me quite some time to understand 😄 So from what I understand, the The proxy seems like the only way best way to handle this. Though at this point, maybe it's easier to have
It should expose the new one. It's a bug that it doesn't since #14890 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Ya... this is getting trickier and trickier. I vote to merge this PR for the next minor and test it during the beta period we are just starting. And then evaluate @bluwy's idea of making createServer
return a proxy for the next major. It would also be easier to explain and document how the server magical instance behaves.
oh, I am very sorry that my description has caused you trouble in understanding the meaning. In order to avoid further misunderstandings, I would like to provide a more detailed explanation of this issue. During the server restart phase, a new dev Server will be created through the vite/packages/vite/src/node/server/index.ts Lines 988 to 992 in a621de8
The configureServer hook will be executed within the _createServer functionvite/packages/vite/src/node/server/index.ts Lines 716 to 718 in a621de8
and at this point, the dev server instance passed to the configureServer hook can be considered as an new initialized dev server instance.vite/packages/vite/src/node/server/index.ts Line 433 in a621de8
After creating a new dev server, existing dev server instances will be reused using the Object.assign method.vite/packages/vite/src/node/server/index.ts Lines 1002 to 1006 in a621de8
Due to reusing the existing dev server instance, the subsequent updates to the properties are on the existing dev server instance rather than a new dev server instance. In the #14890 PR, the _setInternalServer method is used to rebind the dev server instance inside the _createServer function, which is a good idea.vite/packages/vite/src/node/server/index.ts Lines 611 to 615 in a621de8
However, the configureServer hook cannot access the re-bound dev Server instance in the _setInternalServer function due to closure reasons, leading to the problem in #15438.vite/packages/vite/src/node/server/index.ts Line 717 in a621de8
This is also what I mentioned in the description "which was not effective when directly referencing the dev server instance after restarting the server". Directly referencing the dev Server instance cannot access the server in the _setInternalServer function due to closure reasons, and that is the current problem.
The indirect reference to the dev server instance mentioned in the description refers to a situation similar to the following. vite/packages/vite/src/node/server/index.ts Lines 450 to 452 in a621de8
When referencing the dev server instance in the function, the function will be triggered at some point in the future. At this point, the dev server instance instance obtained is the one re-bound in the _setInternalServer method, which is the correct behavior.
It may be a lack of thinking on my part, I don't know why making |
To be clear, I'm not criticising your code or explanations! 😅 I think they're good, but the code with have today (written by many people) made the whole flow hard to grasp, so that took me a while.
I mean that if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I also vote to merge this PR for 5.1. If we don't get any reports about the incompatibility using Proxy, then I think we can try @bluwy's idea with more confidence.
Description
fixes #15438
refs #14890
Additional context
#14890 PR fixed the issue of indirectly referencing the dev
server
instance (e.g. referencing it In the delayed execution function), which was not effective when directly referencing the devserver
instance after restarting the server (e.g. partialmiddlewares
andconfigureServer
hook in the plugin).vite
will expose the devserver
instance to theconfigureServer
hook in the plugin, which also means that the devserver
instance may be used by other hooks in the plugin. Therefore, I think it is necessary to maintain consistency of the devserver
instance after server restart. as for themiddlewares
, I don't think any additional processing is necessary at the moment, as it may result in performance loss and inconsistent behavior before and after configuration. This fix adds a layer of proxy to the devserver
reference in theconfigureServer
hook of the plugin.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).