From 6cfc369a0661df6675bed45d72e01ad2dbb0a1f1 Mon Sep 17 00:00:00 2001 From: bencroker Date: Wed, 13 Nov 2024 12:01:28 +0100 Subject: [PATCH] Complete the fix --- CHANGELOG.md | 2 +- src/services/CacheRequestService.php | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eefcae0c..c14d7c44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixed -- Fixed a bug in which URIs could be incorrectly determined to contain uppercase letters during cache generation. +- Fixed a bug that was preventing cache generation when the “Only Cache Lowercase URIs” setting was enabled. ## 5.9.4 - 2024-11-13 diff --git a/src/services/CacheRequestService.php b/src/services/CacheRequestService.php index 7c13f9e1..60e0538c 100755 --- a/src/services/CacheRequestService.php +++ b/src/services/CacheRequestService.php @@ -196,7 +196,9 @@ public function getIsCacheableSiteUri(?SiteUriModel $siteUri): bool return false; } - if (Blitz::$plugin->settings->onlyCacheLowercaseUris && preg_match('/\p{Lu}/u', $siteUri->uri)) { + $uri = mb_strtolower($siteUri->uri); + + if (Blitz::$plugin->settings->onlyCacheLowercaseUris && $uri !== $siteUri->uri) { Blitz::$plugin->debug('Page not cached because the URI contains uppercase characters.', [], $siteUri->uri); return false; @@ -204,17 +206,17 @@ public function getIsCacheableSiteUri(?SiteUriModel $siteUri): bool // Ignore URIs that are longer than the max URI length $max = Blitz::$plugin->settings->maxUriLength; - if (strlen($siteUri->uri) > $max) { - Blitz::$plugin->debug('Page not cached because it exceeds the max URI length of {max}.', ['max' => $max], $siteUri->uri); + if (strlen($uri) > $max) { + Blitz::$plugin->debug('Page not cached because it exceeds the max URI length of {max}.', ['max' => $max], $uri); return false; } - if ($this->getIsCachedInclude($siteUri->uri)) { + if ($this->getIsCachedInclude($uri)) { return true; } - if ($this->getIsDynamicInclude($siteUri->uri)) { + if ($this->getIsDynamicInclude($uri)) { return false; } @@ -224,12 +226,12 @@ public function getIsCacheableSiteUri(?SiteUriModel $siteUri): bool // Ignore URIs that are resources $resourceBaseUri = trim(parse_url(Craft::getAlias($generalConfig->resourceBaseUrl), PHP_URL_PATH), '/'); - if ($resourceBaseUri && str_starts_with($siteUri->uri, $resourceBaseUri)) { + if ($resourceBaseUri && str_starts_with($uri, $resourceBaseUri)) { return false; } // Ignore URIs that contain `index.php` - if (str_contains($siteUri->uri, 'index.php')) { + if (str_contains($uri, 'index.php')) { Blitz::$plugin->debug('Page not cached because the URL contains `index.php`.', [], $url); return false; @@ -396,6 +398,12 @@ public function getRequestedCacheableSiteUri(): ?SiteUriModel $site = Craft::$app->getSites()->getCurrentSite(); $uri = Craft::$app->getRequest()->getFullUri(); + $queryParams = Craft::$app->getRequest()->getQueryParams(); + $token = Craft::$app->getConfig()->getGeneral()->tokenParam; + if (isset($queryParams[$token])) { + unset($queryParams[$token]); + } + /** * Build the query string from the query params, so that [[Request::getQueryString()]] * doesn’t get called, which is determined from the `$_SERVER` global variable @@ -403,7 +411,7 @@ public function getRequestedCacheableSiteUri(): ?SiteUriModel * * @see Request::getQueryString() */ - $queryString = http_build_query(Craft::$app->getRequest()->getQueryParams()); + $queryString = http_build_query($queryParams); /** * Remove the base site path from the full URI