Skip to content

Commit

Permalink
Merge branch 'release/1.0.35' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jun 13, 2024
2 parents 66b4171 + 10cf583 commit fb7abb7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Plugin Vite Changelog

## 1.0.35 - 2024.06.12
### Added
* By default, only load the Vite AssetBundle if the request is a CP request or a preview request. This can be overridden via the `useForAllRequests` VitePluginService property ([#27](https://github.com/nystudio107/craft-plugin-vite/issues/27))

### Fixed
* Normalize file system paths before fetching them with `file_get_contents()` ([#25](https://github.com/nystudio107/craft-plugin-vite/pull/25))

## 1.0.34 - 2024.03.02
### Fixed
* Fixed an issue where `craft.vite.entry()` would fail if you were using Vite 5 or later, due to the `ManifestHelper::fileNameWithoutHash()` function not working correctly ([#24](https://github.com/nystudio107/craft-plugin-vite/issues/24))
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.34",
"version": "1.0.35",
"keywords": [
"craftcms",
"plugin",
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace nystudio107\pluginvite\helpers;

use Craft;
use craft\helpers\FileHelper as CraftFileHelper;
use craft\helpers\UrlHelper;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
Expand Down Expand Up @@ -85,6 +86,8 @@ function() use ($pathOrUrl, $callback) {
$contents = $response->getBody()->getContents();
}
} else {
// If this is a file path, normalize it first
$pathOrUrl = CraftFileHelper::normalizePath($pathOrUrl);
$contents = @file_get_contents($pathOrUrl);
}
if ($contents && $callback) {
Expand Down
22 changes: 22 additions & 0 deletions src/services/VitePluginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ class VitePluginService extends ViteService
*/
public $pluginDevServerEnvVar = 'VITE_PLUGIN_DEVSERVER';

/**
* @var bool Normally the AssetBundle only needs to be registered for CP and Preview requests, and having it
* not load for frontend requests saves a db write: https://github.com/nystudio107/craft-plugin-vite/issues/27
*/
public $useForAllRequests = false;

/**
* @var array|string[] If the first segment of the request matches any items in the array, load the AssetBundle, too.
* Needed for things that add frontend preview targets like SEOmatic
*/
public $firstSegmentRequests = [
'seomatic',
];

/**
* @inheritDoc
*/
Expand All @@ -60,6 +74,14 @@ public function init()
if (!$this->assetClass || $this->devServerRunning()) {
return;
}
// The Vite service is generally only needed for CP requests & previews, save a db write, see:
// https://github.com/nystudio107/craft-plugin-vite/issues/27
$request = Craft::$app->getRequest();
if (!$this->useForAllRequests && !$request->getIsConsoleRequest()) {
if (!$request->getIsCpRequest() && !$request->getIsPreview() && !in_array($request->getSegment(1), $this->firstSegmentRequests, true)) {
return;
}
}
// Map the $manifestPath and $serverPublic to the hashed `/cpresources/` path & URL for our AssetBundle
$bundle = new $this->assetClass();
$baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl(
Expand Down

0 comments on commit fb7abb7

Please sign in to comment.