From 0ceb26aa133a4eda2d5069e29fd4f7b57a1bc57b Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 May 2024 16:35:56 -0400 Subject: [PATCH 1/4] fix: Normalize file system paths before fetching them with `file_get_contents()` ([#25](https://github.com/nystudio107/craft-plugin-vite/pull/25)) --- src/helpers/FileHelper.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/helpers/FileHelper.php b/src/helpers/FileHelper.php index 11a2e1b..2f5d4d2 100644 --- a/src/helpers/FileHelper.php +++ b/src/helpers/FileHelper.php @@ -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; @@ -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) { From b17ec239941fe5be28b4de86accb242d354004e4 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 May 2024 16:36:05 -0400 Subject: [PATCH 2/4] chore: Version 1.0.35 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3259099..3220f19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Plugin Vite Changelog +## 1.0.35 - UNRELEASED +### 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)) diff --git a/composer.json b/composer.json index adb2dea..9c6824a 100644 --- a/composer.json +++ b/composer.json @@ -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", From 4bf3b8f14571cb3f51c1a9e8b4d847534ead8108 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 12 Jun 2024 20:35:12 -0700 Subject: [PATCH 3/4] feat: 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)) --- src/services/VitePluginService.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/services/VitePluginService.php b/src/services/VitePluginService.php index a481238..ceb09ae 100755 --- a/src/services/VitePluginService.php +++ b/src/services/VitePluginService.php @@ -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 */ @@ -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( From 10cf5837e75cb96c6315ac01c1ca882b5c504594 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 12 Jun 2024 20:37:24 -0700 Subject: [PATCH 4/4] chore: Version 1.0.35 --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3220f19..5bda78f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Plugin Vite Changelog -## 1.0.35 - UNRELEASED +## 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))