@@ -107,6 +107,15 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
107107 let containerImageTagsSeen : Set < string > | undefined ;
108108 let runningContainerIds : Array < string > ;
109109
110+ // This is needed so that we can tell the difference between the Vite http server closing and restarting.
111+ let previousServer : unknown ;
112+ let restartingServer = false ;
113+ function updateRestartingServerFlag ( viteDevServer : unknown ) {
114+ restartingServer =
115+ previousServer !== undefined && viteDevServer !== previousServer ;
116+ previousServer = viteDevServer ;
117+ }
118+
110119 return [
111120 {
112121 name : "vite-plugin-cloudflare" ,
@@ -341,6 +350,7 @@ if (import.meta.hot) {
341350 // Vite `configureServer` Hook
342351 // see https://vite.dev/guide/api-plugin.html#configureserver
343352 async configureServer ( viteDevServer ) {
353+ updateRestartingServerFlag ( viteDevServer ) ;
344354 assertIsNotPreview ( resolvedPluginConfig ) ;
345355
346356 // It is possible to get into a situation where the dev server is restarted by a config file change
@@ -612,6 +622,7 @@ if (import.meta.hot) {
612622 // Vite `configurePreviewServer` Hook
613623 // see https://vite.dev/guide/api-plugin.html#configurepreviewserver
614624 async configurePreviewServer ( vitePreviewServer ) {
625+ updateRestartingServerFlag ( vitePreviewServer ) ;
615626 assertIsPreview ( resolvedPluginConfig ) ;
616627
617628 const inputInspectorPort = await getInputInspectorPortOption (
@@ -705,6 +716,21 @@ if (import.meta.hot) {
705716 containerImageTagsSeen . clear ( ) ;
706717 runningContainerIds = [ ] ;
707718 }
719+
720+ if ( ! restartingServer ) {
721+ debuglog ( "Disposing Miniflare instance" ) ;
722+ await miniflare ?. dispose ( ) . catch ( ( error ) => {
723+ console . error ( "Error disposing Miniflare instance:" , error ) ;
724+ } ) ;
725+ miniflare = undefined ;
726+ } else {
727+ debuglog (
728+ "Vite is restarting so do not dispose of Miniflare instance"
729+ ) ;
730+ }
731+ // Reset the flag so that if a `buildEnd` hook is called again before the next
732+ // configureServer hook then we do dispose of miniflare correctly.
733+ restartingServer = false ;
708734 } ,
709735 } ,
710736 // Plugin to provide a fallback entry file
0 commit comments