From d1c69ed78d6374b042c8fc83d455f0d79830105c Mon Sep 17 00:00:00 2001 From: Simon Rigby <575561+Rigby90@users.noreply.github.com> Date: Fri, 28 Jan 2022 21:49:18 +0000 Subject: [PATCH] fix: improve first load time (#179) --- src/Vite.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Vite.php b/src/Vite.php index 6de13a3..feae398 100644 --- a/src/Vite.php +++ b/src/Vite.php @@ -6,6 +6,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Route; use Illuminate\Support\HtmlString; @@ -225,19 +226,23 @@ protected function shouldUseManifest(): bool */ public function isDevelopmentServerRunning(): bool { - try { - ['host' => $hostname, 'port' => $port] = parse_url(config('vite.ping_url') ?? config('vite.dev_url')); - $connection = @fsockopen($hostname, $port, $errno, $errstr, config('vite.ping_timeout')); + if (isset($this->isDevelopmentServerRunning)) { + return $this->isDevelopmentServerRunning; + } - if (\is_resource($connection)) { - fclose($connection); + $url = config('vite.ping_url') ?? config('vite.dev_url'); - return true; - } - } catch (\Throwable $th) { + try { + /** + * The below will throw an exception if no dev server is running. + */ + Http::get($url); + + return $this->isDevelopmentServerRunning = true; + } catch (\Throwable $e) { } - return false; + return $this->isDevelopmentServerRunning = false; } /**