Skip to content

Commit baa0f3e

Browse files
committed
fix: Fixed an issue where checkDevServer didn't work with Vite 3, because they removed the intercepting of __vite_ping ([#37](nystudio107/craft-vite#37))
1 parent 3e4500b commit baa0f3e

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

src/helpers/FileHelper.php

+36-26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use craft\helpers\UrlHelper;
1515
use GuzzleHttp\Client;
1616
use GuzzleHttp\RequestOptions;
17+
use Psr\Http\Message\ResponseInterface;
1718
use Throwable;
1819
use yii\caching\ChainedDependency;
1920
use yii\caching\FileDependency;
@@ -78,45 +79,54 @@ public static function fetch(string $pathOrUrl, callable $callback = null, strin
7879
self::CACHE_KEY . $cacheKeySuffix . $pathOrUrl,
7980
function () use ($pathOrUrl, $callback) {
8081
$contents = null;
81-
$result = null;
8282
if (UrlHelper::isAbsoluteUrl($pathOrUrl)) {
83-
// See if we can connect to the server
84-
$clientOptions = [
85-
RequestOptions::HTTP_ERRORS => false,
86-
RequestOptions::CONNECT_TIMEOUT => 3,
87-
RequestOptions::VERIFY => false,
88-
RequestOptions::TIMEOUT => 5,
89-
];
90-
$client = new Client($clientOptions);
91-
try {
92-
$response = $client->request('GET', $pathOrUrl, [
93-
RequestOptions::HEADERS => [
94-
'Accept' => '*/*',
95-
],
96-
]);
97-
if ($response->getStatusCode() === 200) {
98-
$contents = $response->getBody()->getContents();
99-
}
100-
} catch (Throwable $e) {
101-
Craft::error($e->getMessage(), __METHOD__);
83+
$response = self::fetchResponse($pathOrUrl);
84+
if ($response && $response->getStatusCode() === 200) {
85+
$contents = $response->getBody()->getContents();
10286
}
10387
} else {
10488
$contents = @file_get_contents($pathOrUrl);
10589
}
106-
if ($contents) {
107-
$result = $contents;
108-
if ($callback) {
109-
$result = $callback($result);
110-
}
90+
if ($contents && $callback) {
91+
$contents = $callback($contents);
11192
}
11293

113-
return $result;
94+
return $contents;
11495
},
11596
$cacheDuration,
11697
$dependency
11798
);
11899
}
119100

101+
/**
102+
* Return a Guzzle ResponseInterface for the passed in $url
103+
*
104+
* @param string $url
105+
* @return ResponseInterface|null
106+
*/
107+
public static function fetchResponse(string $url): ?ResponseInterface
108+
{
109+
$response = null;
110+
$clientOptions = [
111+
RequestOptions::HTTP_ERRORS => false,
112+
RequestOptions::CONNECT_TIMEOUT => 3,
113+
RequestOptions::VERIFY => false,
114+
RequestOptions::TIMEOUT => 5,
115+
];
116+
$client = new Client($clientOptions);
117+
try {
118+
$response = $client->request('GET', $url, [
119+
RequestOptions::HEADERS => [
120+
'Accept' => '*/*',
121+
],
122+
]);
123+
} catch (Throwable $e) {
124+
Craft::error($e->getMessage(), __METHOD__);
125+
}
126+
127+
return $response;
128+
}
129+
120130
/**
121131
* Combine a path with a URL to create a URL
122132
*

src/services/ViteService.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ public function devServerRunning(): bool
172172
}
173173
// Check to see if the dev server is actually running by pinging it
174174
$url = FileHelper::createUrl($this->devServerInternal, self::VITE_DEVSERVER_PING);
175-
$this->devServerRunningCached = !($this->fetch($url) === null);
175+
$response = FileHelper::fetchResponse($url);
176+
$this->devServerRunningCached = false;
177+
// Status code of 200 or 404 means the dev server is running
178+
if ($response) {
179+
$statusCode = $response->getStatusCode();
180+
$this->devServerRunningCached = $statusCode === 200 || $statusCode === 404;
181+
}
176182

177183
return $this->devServerRunningCached;
178184
}
@@ -301,12 +307,12 @@ public function entry(string $path): string
301307
*
302308
* @return string
303309
*/
304-
public function asset(string $path, bool $public=false): string
310+
public function asset(string $path, bool $public = false): string
305311
{
306312
if ($this->devServerRunning()) {
307313
return $this->devServerAsset($path);
308314
}
309-
315+
310316
if ($public) {
311317
return $this->publicAsset($path);
312318
}
@@ -326,7 +332,7 @@ public function devServerAsset(string $path): string
326332
// Return a URL to the given asset
327333
return FileHelper::createUrl($this->devServerPublic, $path);
328334
}
329-
335+
330336
/**
331337
* Return the URL for the asset from the public Vite folder
332338
*

0 commit comments

Comments
 (0)