Skip to content

Commit e74742c

Browse files
committed
MAGE-1245 Add logic to check for empty controller on page HIT with no dispatch
1 parent 169ae34 commit e74742c

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

Plugin/RenderingCacheContextPlugin.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Magento\Framework\App\Request\Http;
88
use Magento\Framework\Exception\NoSuchEntityException;
99
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\UrlRewrite\Model\UrlFinderInterface;
1011

1112
/**
1213
* The purpose of this class is to render different cached versions of the pages according to the user agent.
@@ -23,7 +24,8 @@ class RenderingCacheContextPlugin
2324
public function __construct(
2425
protected ConfigHelper $configHelper,
2526
protected StoreManagerInterface $storeManager,
26-
protected Http $request
27+
protected Http $request,
28+
protected UrlFinderInterface $urlFinder
2729
) { }
2830

2931
/**
@@ -37,7 +39,7 @@ public function __construct(
3739
* @throws NoSuchEntityException
3840
*/
3941
public function beforeGetVaryString(HttpContext $subject): array {
40-
if (!$this->applyCacheContext()) {
42+
if (!$this->shouldApplyCacheContext()) {
4143
return [];
4244
}
4345

@@ -54,13 +56,39 @@ public function beforeGetVaryString(HttpContext $subject): array {
5456
return [];
5557
}
5658

59+
/**
60+
* @param int $storeId
61+
* @return string
62+
*/
63+
protected function getOriginalRoute(int $storeId): string
64+
{
65+
$requestUri = $this->request->getRequestUri();
66+
67+
$rewrite = $this->urlFinder->findOneByData([
68+
'request_path' => ltrim($requestUri, '/'),
69+
'store_id' => $storeId,
70+
]);
71+
72+
return $rewrite?->getTargetPath() ?? "";
73+
}
74+
75+
/**
76+
* @param int $storeId
77+
* @return bool
78+
*/
79+
protected function isCategoryPage(int $storeId): bool
80+
{
81+
$controller = $this->request->getControllerName();
82+
return $controller === 'category' || str_starts_with($this->getOriginalRoute($storeId), 'catalog/category');
83+
}
84+
5785
/**
5886
* @return bool
5987
* @throws NoSuchEntityException
6088
*/
61-
protected function applyCacheContext(): bool
89+
protected function shouldApplyCacheContext(): bool
6290
{
6391
$storeId = $this->storeManager->getStore()->getId();
64-
return $this->request->getControllerName() == 'category' && $this->configHelper->replaceCategories($storeId);
92+
return $this->isCategoryPage($storeId) && $this->configHelper->replaceCategories($storeId);
6593
}
6694
}

0 commit comments

Comments
 (0)