Skip to content

Plugin API: Dev server close/reload #17285

Closed
Closed

Description

@thommyhh

Description

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.

Alternative

No response

Additional context

No response

Validations

Activity

  1. hi-ogawa commented on May 24, 2024

    @hi-ogawa
    Contributor
  2. thommyhh commented on May 24, 2024

    @thommyhh
    Author

    @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.

  3. aaronadamsCA commented on Jan 26, 2026

    @aaronadamsCA

    I need to start the server already inside the config hook.

    We have the same issue:

    1. In config, start a listener and push its hostname to allowedHosts.
    2. In configureServer, in httpServer.on("listening") callback, start forwarding to the HTTP server.
    3. 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.

  4. sapphi-red commented on Oct 9, 2026

    @sapphi-red
    Member
  5. locked and limited conversation to collaborators on Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions