-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
feat: add vite client connect events #20978
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
feat: add vite client connect events #20978
Conversation
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.
Looks good to me. Should we update docs to mention this? I see currently there is an example like server.on('connection', ...). The difference from current hot.on("connection" ...) should be probably mentioned too or how it can be entirely replaced? (and that's something I'm actually confused about)
packages/vite/types/customEvent.d.ts
Outdated
| // server events | ||
| 'vite:client-connect': undefined | ||
| 'vite:client-disconnect': undefined |
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.
Should we rename it as vite:client:connect so it's similar to vite:ws:connect?
Btw I found it initially weird that we're mixing client/server events here, but it's not too terrible at the end if it makes configuring events easier. It's unlikely that client <-> server with the same event name will need different payload shape.
commit: |
The difference is only the callback parameter. |
|
For custom hmr channel/transport, I think the mechanism of emitting |
|
Added the docs and renamed the event 👍 |
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.
I don't think this is an issue but let me note one observation.
Currently, the default ssr environment server.environments.ssr.hot.on("vite:client:connect", ...) is triggered when server.environment.ssr.runner getter is accessed (or manually createServerModuleRunner is called) and the timing might look arbitrary. For example, one plugin can do ....ssr.hot.on("vite:client:connect" ...) during configureServer (like in the test) and other plugins might also grab ....ssr.runner during its configureServer.
For node worker example (which currently ignores vite:client:connect), I think the same can be said and the implementation might be able to allow somehow lazily spawning workers and also allow multiple workers with different entrypoints, If such implementation is possible, it probably makes sense to emit vite:client:connect for each module runner instantiation or entrypoint import on worker side. (I'm vaguely imagining how ?nodeWorker could be implemented #3932).
| // client is already connected | ||
| if (event === 'connection' || event === 'vite:client:connect') return |
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.
Related to the last comment, I'm wondering if it's more natural to emit vite:client:connect from "client" side. For example, what does "server" side see if we add a following in worker.js above?
runner.hmrClient.send({ type: "custom", event: "vite:client:connected" })Also what if we do the same in hmrClient.send(...) in @vite/client?
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.
It would work for vite:client:connect but it won't work for vite:client:disconnect. I think it's better to handle them on the same side.
Indeed. I think I need to figure out when it should be emitted and probably how to handle the missed events (the clients connected before listening to the event). Solving #19710 should give me a better idea of how to handle it. |
|
Somewhat related to missed events, would it make sense (or is it possible) to expose currently connected clients |
|
Yeah, I thought about that. I think it makes sense, but I'd like to leave that for now. (if you have a concrete usecase, we can add it now) |
|
Leaving it out sounds good to me 👍 No use case in mind but was curious. |
I'm reading the new docs and I'm still a bit confused between vite/packages/vite/src/node/server/hmr.ts Line 148 in ca18b23
If they are the same, should we deprecate the |
The |
bluwy
left a comment
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.
LGTM. Thanks for updating!
Description
Added
vite:client-connect/vite:client-disconnectevents so that it's possible to track the client life-cycle. This is needed for full bundle mode to properly trigger HMR (we have the same issue in unbundled mode as well, but it wasn't a big problem). This is probably needed to solve #19710 as well.This API is also useful for
vite-dev-rpcto migrate toenvironment.hot.