Skip to content

feat: add closeServer and closePreviewServer hooks - #23110

Merged
sapphi-red merged 4 commits into
vitejs:mainfrom
jamesopstad:close-server-hook
Aug 21, 2026
Merged

feat: add closeServer and closePreviewServer hooks#23110
sapphi-red merged 4 commits into
vitejs:mainfrom
jamesopstad:close-server-hook

Conversation

@jamesopstad

@jamesopstad jamesopstad commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #22913

Description

Adds two new plugin hooks that let plugins run cleanup when a dev or preview server shuts down:

  • closeServer(server, { reason }) — called when the dev server closes. reason is 'restart' (server is being replaced by a restart) or 'close' (server is shutting down for good).
  • closePreviewServer(server) — called when the preview server closes. No reason, since preview servers never restart.
{
  name: 'my-plugin',
  closeServer(server, { reason }) {
    if (reason === 'close') {
      // cleanup etc.
    }
  },
  closePreviewServer(server) {
    // cleanup etc.
  },
}

Notes

  • The hooks run only once the server has been fully torn down and the HTTP server, WebSocket server, environments etc. have all closed. This means there are no in-flight requests or open connections to race against.
  • The hooks are global, async and parallel and use Promise.all. This matches the global configResolved() hook and the per-environment closeWatcher() hook. It may be preferable to use Promise.allSettled but, if so, this behaviour should be aligned across these hooks. Currently a throwing closeServer() hook can lead to unhandled rejections and a dormant server instance.

@jamesopstad
jamesopstad marked this pull request as ready for review July 30, 2026 18:10
@sapphi-red sapphi-red added p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) feat: dev dev server feat: preview vite preview labels Jul 31, 2026
@sapphi-red

Copy link
Copy Markdown
Member

Would it be possible to extract the signal-exit change in a separate PR?

@jamesopstad jamesopstad changed the title feat: close server hooks feat: add closeServer and closePreviewServer hooks Aug 3, 2026
@jamesopstad

Copy link
Copy Markdown
Contributor Author

Would it be possible to extract the signal-exit change in a separate PR?

I've updated this PR to only add the new hooks and I have a separate PR to use signal-exit that I can open once this one's merged.

One thing to flag is that a throwing closeServer hook rejects on restart (after teardown, before the new server listens), leaving the server dormant. The config-change path logs it and the r shortcut surfaces it as an unhandled rejection. Should we handle this differently?

@sapphi-red

Copy link
Copy Markdown
Member

One thing to flag is that a throwing closeServer hook rejects on restart (after teardown, before the new server listens), leaving the server dormant. The config-change path logs it and the r shortcut surfaces it as an unhandled rejection. Should we handle this differently?

The reason why the config-change prevents the unhandled rejection is to avoid the process exit happening when saving a intermediate config file. I think it's ok to keep the difference for this PR. But maybe we should align it.

Comment thread packages/vite/src/node/__tests__/plugins/hooks.spec.ts Outdated
@sapphi-red sapphi-red added this to the 8.3 milestone Aug 4, 2026
@github-project-automation github-project-automation Bot moved this to Discussing in Team Board Aug 4, 2026
sapphi-red
sapphi-red previously approved these changes Aug 4, 2026
@sapphi-red sapphi-red moved this from Discussing to Approved in Team Board Aug 5, 2026
Comment thread docs/guide/api-plugin.md Outdated
@sapphi-red
sapphi-red merged commit e17d2d5 into vitejs:main Aug 21, 2026
17 of 18 checks passed
This was referenced Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: dev dev server feat: preview vite preview p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Provide an API to run logic when the server is restarted or closed

3 participants