@@ -17,7 +17,23 @@ class Vite
1717
1818 public static function running (): bool
1919 {
20- return Filesystem::exists ('app/storage/framework/runtime/vite.hot ' );
20+ $ hotFile = 'app/storage/framework/runtime/vite.hot ' ;
21+
22+ // If hot file doesn't exist, Vite is definitely not running
23+ if (! Filesystem::exists ($ hotFile )) {
24+ return false ;
25+ }
26+
27+ // Validate that Vite is actually running by checking if port 5173 is in use
28+ if (static ::isVitePortActive ()) {
29+ return true ;
30+ }
31+
32+ // If hot file exists but port is not active, clean up stale file
33+ // This handles cases where processes didn't clean up properly (common on Windows)
34+ static ::cleanUpStaleHotFile ($ hotFile );
35+
36+ return false ;
2137 }
2238
2339 public static function asset (string $ path ): HtmlString
@@ -76,4 +92,44 @@ protected static function formatScriptInclude(string $path): string
7692 {
7793 return sprintf ('<script src="http://localhost:5173/%s" type="module"></script> ' , $ path );
7894 }
95+
96+ /**
97+ * Check if Vite development server is actually running on port 5173.
98+ */
99+ protected static function isVitePortActive (): bool
100+ {
101+ $ socket = @fsockopen ('localhost ' , 5173 , $ errno , $ errstr , 1 );
102+
103+ if ($ socket !== false ) {
104+ fclose ($ socket );
105+ return true ;
106+ }
107+
108+ return false ;
109+ }
110+
111+ /**
112+ * Clean up stale vite.hot file with Windows-compatible handling.
113+ */
114+ protected static function cleanUpStaleHotFile (string $ hotFile ): void
115+ {
116+ try {
117+ // On Windows, files might be locked, so we attempt deletion with retries
118+ $ maxAttempts = PHP_OS_FAMILY === 'Windows ' ? 3 : 1 ;
119+
120+ for ($ attempt = 1 ; $ attempt <= $ maxAttempts ; $ attempt ++) {
121+ if (Filesystem::delete ($ hotFile )) {
122+ break ;
123+ }
124+
125+ // If on Windows and not the last attempt, wait a bit before retrying
126+ if (PHP_OS_FAMILY === 'Windows ' && $ attempt < $ maxAttempts ) {
127+ usleep (100000 ); // 100ms delay
128+ }
129+ }
130+ } catch (\Exception $ exception ) {
131+ // Silently handle cleanup failures - the stale file will be checked again next time
132+ // This prevents the application from breaking due to file system issues
133+ }
134+ }
79135}
0 commit comments