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
I've developed a plugin that runs fractal in the background and configures Vite to proxy some URLs to it. This works fine, as long as the Vite config is not changed.
When the Vite config is changed, the Vite dev server is restarted. Unfortunately there is no way of shutting down the fractal server. A new instance is created, because the plugin is recreated. Starting the fractal server failed, because the port is already in use.
Suggested solution
Add a plugin method closeServer, which is called, when the dev server is closed/restarted, which then waits for all plugins to complete their actions.
If there is already a solution to this, I appreciate any hints.
@hi-ogawa Thank your for the hint. I tried that before and just some more testing. It looks like the hooks are executed like config, configureServer, buildEnd (from the old run), buildStart. I need a variable from the started server inside my config, so I need to start the server already inside the config hook.
However, I solved it by finding the necessary number of empty ports before starting the new server process. This way the ports switch with every reload, but that's ok, as the user never uses these ports directly.
I need to start the server already inside the config hook.
We have the same issue:
In config, start a listener and push its hostname to allowedHosts.
In configureServer, in httpServer.on("listening") callback, start forwarding to the HTTP server.
Never close the listener. 😕
buildEnd, httpServer.on("close"), etc. all seem to run on server restart as well as Vite exit, so if we use those, the server breaks on restart. We can only create the listener once, so we only want to dispose of it once when Vite is exiting.
EDIT:
What if we're thinking about this the wrong way around?
Maybe the bad assumption here is that it's ever okay to start a server inside the config hook. I've already found a ton of ways this breaks, since lots of things resolve the Vite config in ways you might not assume.
Instead, what if could start my listener in httpServer.on("listening"), then somehow push its hostname to Vite's allowedHosts?
Maybe there's some small set of options like this that Vite should allow to be updated after the server is already running.
Activity
hi-ogawa commentedon May 24, 2024
Have you tried
buildEnd? I often use it to teardown resources like this https://github.com/hi-ogawa/experiments/blob/a9c34a135b6c3ff62a3d763ff8aa8981216cc79a/tsr-rsc/src/integrations/plugin.ts#L67-L79thommyhh commentedon May 24, 2024
@hi-ogawa Thank your for the hint. I tried that before and just some more testing. It looks like the hooks are executed like
config,configureServer,buildEnd(from the old run),buildStart. I need a variable from the started server inside my config, so I need to start the server already inside theconfighook.However, I solved it by finding the necessary number of empty ports before starting the new server process. This way the ports switch with every reload, but that's ok, as the user never uses these ports directly.
aaronadamsCA commentedon Jan 26, 2026
We have the same issue:
config, start a listener and push its hostname toallowedHosts.configureServer, inhttpServer.on("listening")callback, start forwarding to the HTTP server.buildEnd,httpServer.on("close"), etc. all seem to run on server restart as well as Vite exit, so if we use those, the server breaks on restart. We can only create the listener once, so we only want to dispose of it once when Vite is exiting.EDIT:
What if we're thinking about this the wrong way around?
Maybe the bad assumption here is that it's ever okay to start a server inside the
confighook. I've already found a ton of ways this breaks, since lots of things resolve the Vite config in ways you might not assume.Instead, what if could start my listener in
httpServer.on("listening"), then somehow push its hostname to Vite'sallowedHosts?Maybe there's some small set of options like this that Vite should allow to be updated after the server is already running.
sapphi-red commentedon Oct 9, 2026
#23110