Skip to content

Commit 0a766d3

Browse files
committed
MAGE-1245 Restore getData implementation with improved data consistency checks
1 parent e74742c commit 0a766d3

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

Plugin/RenderingCacheContextPlugin.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class RenderingCacheContextPlugin
2121
public const RENDERING_WITH_BACKEND = 'with_backend';
2222
public const RENDERING_WITHOUT_BACKEND = 'without_backend';
2323

24+
public const CATEGORY_CONTROLLER = 'category';
25+
public const CATEGORY_ROUTE = 'catalog/category/view';
26+
2427
public function __construct(
2528
protected ConfigHelper $configHelper,
2629
protected StoreManagerInterface $storeManager,
@@ -29,31 +32,31 @@ public function __construct(
2932
) { }
3033

3134
/**
32-
* Add a rendering context to the vary string to distinguish how which versions of the category PLP should be cached
35+
* Add a rendering context to the vary string data to distinguish which versions of the category PLP should be cached
3336
* (If the "prevent backend rendering" configuration is enabled and the user agent is not whitelisted to display it,
3437
* we set a different page variation, and the FPC stores a different cached page)
3538
*
36-
* @param HttpContext $subject
39+
* IMPORTANT:
40+
* Magento\Framework\App\Http\Context::getData can be called multiple times over the course of the request lifecycle
41+
* it is important that this plugin return the data consistently - or the cache will be invalidated unexpectedly!
3742
*
43+
* @param HttpContext $subject
44+
* @param array $data
3845
* @return array original params
3946
* @throws NoSuchEntityException
4047
*/
41-
public function beforeGetVaryString(HttpContext $subject): array {
48+
public function afterGetData(HttpContext $subject, array $data): array {
4249
if (!$this->shouldApplyCacheContext()) {
43-
return [];
50+
return $data;
4451
}
4552

4653
$context = $this->configHelper->preventBackendRendering() ?
4754
self::RENDERING_WITHOUT_BACKEND :
4855
self::RENDERING_WITH_BACKEND;
4956

50-
$subject->setValue(
51-
self::RENDERING_CONTEXT,
52-
$context,
53-
$context
54-
);
57+
$data[self::RENDERING_CONTEXT] = $context;
5558

56-
return [];
59+
return $data;
5760
}
5861

5962
/**
@@ -73,13 +76,24 @@ protected function getOriginalRoute(int $storeId): string
7376
}
7477

7578
/**
79+
* @param string $path
80+
* @return bool
81+
*/
82+
protected function isCategoryRoute(string $path): bool {
83+
return str_starts_with($path, self::CATEGORY_ROUTE);
84+
}
85+
86+
/**
87+
* This can be called in a variety of contexts - so this should always be called consistently
88+
*
7689
* @param int $storeId
7790
* @return bool
7891
*/
7992
protected function isCategoryPage(int $storeId): bool
8093
{
81-
$controller = $this->request->getControllerName();
82-
return $controller === 'category' || str_starts_with($this->getOriginalRoute($storeId), 'catalog/category');
94+
return $this->request->getControllerName() === self::CATEGORY_CONTROLLER
95+
|| $this->isCategoryRoute($this->request->getRequestUri())
96+
|| $this->isCategoryRoute($this->getOriginalRoute($storeId));
8397
}
8498

8599
/**

Test/Integration/Category/CategoryCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CategoryCacheTest extends \Magento\TestFramework\TestCase\AbstractControll
1717
public static function getCategoryProvider(): array
1818
{
1919
return [
20-
['categoryId' => 20, 'name' => 'Women'], // not a category controller
20+
['categoryId' => 20, 'name' => 'Women'],
2121
['categoryId' => 21, 'name' => 'Women > Tops'],
2222
];
2323
}

0 commit comments

Comments
 (0)