From 77218d8b5eeff08caa7fd180a837938bcd082d3b Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 May 2024 16:35:37 -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 3cfc563..d0fb392 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 ed0da2e3a81cc137e5750b10f01ef4b8d9722f1f Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 22 May 2024 16:35:44 -0400 Subject: [PATCH 2/4] chore: Version 4.0.11 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2993d4..a281d72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Plugin Vite Changelog +## 4.0.11 - UNRELEASED +### Fixed +* Normalize file system paths before fetching them with `file_get_contents()` ([#25](https://github.com/nystudio107/craft-plugin-vite/pull/25)) + ## 4.0.10 - 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 c55d15a..939c55a 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": "4.0.10", + "version": "4.0.11", "keywords": [ "craftcms", "plugin", From 3fb0b92f8304c5c465f68b5a7d87805817c92eef Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 12 Jun 2024 20:33:55 -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 ec718ea..06738d4 100755 --- a/src/services/VitePluginService.php +++ b/src/services/VitePluginService.php @@ -40,6 +40,20 @@ class VitePluginService extends ViteService */ public string $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 bool $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 array $firstSegmentRequests = [ + 'seomatic', + ]; + /** * @inheritDoc */ @@ -62,6 +76,14 @@ public function init(): void 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 66c082b738d8826780cca7e362af58bf55609648 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 12 Jun 2024 20:38:49 -0700 Subject: [PATCH 4/4] chore: Version 4.0.11 --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a281d72..22f8e0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Plugin Vite Changelog -## 4.0.11 - UNRELEASED +## 4.0.11 - 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))