Skip to content

Commit

Permalink
Merge branch 'release/1.0.27' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jul 16, 2022
2 parents 3e4500b + 4a1804c commit 0a1dfe9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Plugin Vite Changelog

## 1.0.27 - 2022.07.16
### Changed
* Fixed an issue where `checkDevServer` didn't work with Vite 3, because they removed the intercepting of `__vite_ping` ([#37](https://github.com/nystudio107/craft-vite/issues/37))

## 1.0.26 - 2022.06.29
### Changed
* Adds a boolean as a second param to the `craft.vite.asset(url, true)` so that assets in the vite public folder can be referenced correctly from Twig ([#10](https://github.com/nystudio107/craft-plugin-vite/pull/10))
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nystudio107/craft-plugin-vite",
"description": "Plugin Vite is the conduit between Craft CMS plugins and Vite, with manifest.json & HMR support",
"version": "1.0.26",
"version": "1.0.27",
"keywords": [
"craftcms",
"plugin",
Expand Down
62 changes: 36 additions & 26 deletions src/helpers/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use craft\helpers\UrlHelper;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use yii\caching\ChainedDependency;
use yii\caching\FileDependency;
Expand Down Expand Up @@ -78,45 +79,54 @@ public static function fetch(string $pathOrUrl, callable $callback = null, strin
self::CACHE_KEY . $cacheKeySuffix . $pathOrUrl,
function () use ($pathOrUrl, $callback) {
$contents = null;
$result = null;
if (UrlHelper::isAbsoluteUrl($pathOrUrl)) {
// See if we can connect to the server
$clientOptions = [
RequestOptions::HTTP_ERRORS => false,
RequestOptions::CONNECT_TIMEOUT => 3,
RequestOptions::VERIFY => false,
RequestOptions::TIMEOUT => 5,
];
$client = new Client($clientOptions);
try {
$response = $client->request('GET', $pathOrUrl, [
RequestOptions::HEADERS => [
'Accept' => '*/*',
],
]);
if ($response->getStatusCode() === 200) {
$contents = $response->getBody()->getContents();
}
} catch (Throwable $e) {
Craft::error($e->getMessage(), __METHOD__);
$response = self::fetchResponse($pathOrUrl);
if ($response && $response->getStatusCode() === 200) {
$contents = $response->getBody()->getContents();
}
} else {
$contents = @file_get_contents($pathOrUrl);
}
if ($contents) {
$result = $contents;
if ($callback) {
$result = $callback($result);
}
if ($contents && $callback) {
$contents = $callback($contents);
}

return $result;
return $contents;
},
$cacheDuration,
$dependency
);
}

/**
* Return a Guzzle ResponseInterface for the passed in $url
*
* @param string $url
* @return ResponseInterface|null
*/
public static function fetchResponse(string $url): ?ResponseInterface
{
$response = null;
$clientOptions = [
RequestOptions::HTTP_ERRORS => false,
RequestOptions::CONNECT_TIMEOUT => 3,
RequestOptions::VERIFY => false,
RequestOptions::TIMEOUT => 5,
];
$client = new Client($clientOptions);
try {
$response = $client->request('GET', $url, [
RequestOptions::HEADERS => [
'Accept' => '*/*',
],
]);
} catch (Throwable $e) {
Craft::error($e->getMessage(), __METHOD__);
}

return $response;
}

/**
* Combine a path with a URL to create a URL
*
Expand Down
14 changes: 10 additions & 4 deletions src/services/ViteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ public function devServerRunning(): bool
}
// Check to see if the dev server is actually running by pinging it
$url = FileHelper::createUrl($this->devServerInternal, self::VITE_DEVSERVER_PING);
$this->devServerRunningCached = !($this->fetch($url) === null);
$response = FileHelper::fetchResponse($url);
$this->devServerRunningCached = false;
// Status code of 200 or 404 means the dev server is running
if ($response) {
$statusCode = $response->getStatusCode();
$this->devServerRunningCached = $statusCode === 200 || $statusCode === 404;
}

return $this->devServerRunningCached;
}
Expand Down Expand Up @@ -301,12 +307,12 @@ public function entry(string $path): string
*
* @return string
*/
public function asset(string $path, bool $public=false): string
public function asset(string $path, bool $public = false): string
{
if ($this->devServerRunning()) {
return $this->devServerAsset($path);
}

if ($public) {
return $this->publicAsset($path);
}
Expand All @@ -326,7 +332,7 @@ public function devServerAsset(string $path): string
// Return a URL to the given asset
return FileHelper::createUrl($this->devServerPublic, $path);
}

/**
* Return the URL for the asset from the public Vite folder
*
Expand Down

0 comments on commit 0a1dfe9

Please sign in to comment.