@@ -21,6 +21,9 @@ class RenderingCacheContextPlugin
21
21
public const RENDERING_WITH_BACKEND = 'with_backend ' ;
22
22
public const RENDERING_WITHOUT_BACKEND = 'without_backend ' ;
23
23
24
+ public const CATEGORY_CONTROLLER = 'category ' ;
25
+ public const CATEGORY_ROUTE = 'catalog/category/view ' ;
26
+
24
27
public function __construct (
25
28
protected ConfigHelper $ configHelper ,
26
29
protected StoreManagerInterface $ storeManager ,
@@ -29,31 +32,31 @@ public function __construct(
29
32
) { }
30
33
31
34
/**
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
33
36
* (If the "prevent backend rendering" configuration is enabled and the user agent is not whitelisted to display it,
34
37
* we set a different page variation, and the FPC stores a different cached page)
35
38
*
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!
37
42
*
43
+ * @param HttpContext $subject
44
+ * @param array $data
38
45
* @return array original params
39
46
* @throws NoSuchEntityException
40
47
*/
41
- public function beforeGetVaryString (HttpContext $ subject ): array {
48
+ public function afterGetData (HttpContext $ subject, array $ data ): array {
42
49
if (!$ this ->shouldApplyCacheContext ()) {
43
- return [] ;
50
+ return $ data ;
44
51
}
45
52
46
53
$ context = $ this ->configHelper ->preventBackendRendering () ?
47
54
self ::RENDERING_WITHOUT_BACKEND :
48
55
self ::RENDERING_WITH_BACKEND ;
49
56
50
- $ subject ->setValue (
51
- self ::RENDERING_CONTEXT ,
52
- $ context ,
53
- $ context
54
- );
57
+ $ data [self ::RENDERING_CONTEXT ] = $ context ;
55
58
56
- return [] ;
59
+ return $ data ;
57
60
}
58
61
59
62
/**
@@ -73,13 +76,24 @@ protected function getOriginalRoute(int $storeId): string
73
76
}
74
77
75
78
/**
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
+ *
76
89
* @param int $storeId
77
90
* @return bool
78
91
*/
79
92
protected function isCategoryPage (int $ storeId ): bool
80
93
{
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 ));
83
97
}
84
98
85
99
/**
0 commit comments