Skip to content

Commit b42adba

Browse files
committed
Replace slow socket call with hot file check
1 parent cb56508 commit b42adba

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

packages/framework/src/Facades/Vite.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,8 @@ public static function running(): bool
1818
return true;
1919
}
2020

21-
// Check if Vite dev server is running by attempting to connect to it
22-
// Todo: Improve performance on Windows (takes less than 1ms on macOS, but around 100ms on Windows)
23-
set_error_handler(fn () => false); // Todo: This warning surpressor does not work on Windows
24-
$server = fsockopen('localhost', 5173, $errno, $errstr, 0.1);
25-
restore_error_handler();
26-
27-
if ($server) {
28-
fclose($server);
29-
30-
return true;
31-
}
32-
33-
return false;
21+
// Check for Vite hot file
22+
return Filesystem::exists('app/storage/framework/cache/vite.hot');
3423
}
3524

3625
public static function assets(array $paths): HtmlString

packages/framework/tests/Unit/Facades/ViteFacadeTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44

55
use Hyde\Testing\UnitTestCase;
66
use Hyde\Facades\Vite;
7+
use Hyde\Testing\CreatesTemporaryFiles;
78

89
/**
910
* @covers \Hyde\Facades\Vite
1011
*/
1112
class ViteFacadeTest extends UnitTestCase
1213
{
14+
use CreatesTemporaryFiles;
15+
16+
protected static bool $needsKernel = true;
17+
18+
protected function tearDown(): void
19+
{
20+
$this->cleanUpFilesystem();
21+
}
22+
1323
public function testRunningReturnsTrueWhenEnvironmentVariableIsSet()
1424
{
1525
putenv('HYDE_SERVER_VITE=enabled');
@@ -19,17 +29,14 @@ public function testRunningReturnsTrueWhenEnvironmentVariableIsSet()
1929
putenv('HYDE_SERVER_VITE');
2030
}
2131

22-
public function testRunningReturnsTrueWhenViteServerIsAccessible()
32+
public function testRunningReturnsTrueWhenViteHotFileExists()
2333
{
24-
// Create a mock server to simulate Vite
25-
$server = stream_socket_server('tcp://localhost:5173');
34+
$this->file('app/storage/framework/cache/vite.hot');
2635

2736
$this->assertTrue(Vite::running());
28-
29-
stream_socket_shutdown($server, STREAM_SHUT_RDWR);
3037
}
3138

32-
public function testRunningReturnsFalseWhenViteServerIsNotAccessible()
39+
public function testRunningReturnsFalseWhenViteHotFileDoesNotExist()
3340
{
3441
$this->assertFalse(Vite::running());
3542
}

0 commit comments

Comments
 (0)