5
5
use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
6
6
use Magento \Framework \App \Http \Context as HttpContext ;
7
7
use Magento \Framework \App \Request \Http ;
8
+ use Magento \Framework \Exception \NoSuchEntityException ;
8
9
use Magento \Store \Model \StoreManagerInterface ;
10
+ use Magento \UrlRewrite \Model \UrlFinderInterface ;
9
11
10
12
/**
11
13
* The purpose of this class is to render different cached versions of the pages according to the user agent.
15
17
*/
16
18
class RenderingCacheContextPlugin
17
19
{
18
- public const RENDERING_CONTEXT = 'rendering_context ' ;
20
+ public const RENDERING_CONTEXT = 'algolia_rendering_context ' ;
19
21
public const RENDERING_WITH_BACKEND = 'with_backend ' ;
20
22
public const RENDERING_WITHOUT_BACKEND = 'without_backend ' ;
21
23
22
- private $ configHelper ;
23
- private $ storeManager ;
24
- private $ request ;
24
+ public const CATEGORY_CONTROLLER = 'category ' ;
25
+ public const CATEGORY_ROUTE = 'catalog/category/view ' ;
25
26
26
27
public function __construct (
27
- ConfigHelper $ configHelper ,
28
- StoreManagerInterface $ storeManager ,
29
- Http $ request
30
- ) {
31
- $ this ->configHelper = $ configHelper ;
32
- $ this ->storeManager = $ storeManager ;
33
- $ this ->request = $ request ;
34
- }
28
+ protected ConfigHelper $ configHelper ,
29
+ protected StoreManagerInterface $ storeManager ,
30
+ protected Http $ request ,
31
+ protected UrlFinderInterface $ urlFinder
32
+ ) { }
35
33
36
34
/**
37
- * Add Rendering context for caching purposes
38
- * (If the prevent rendering configuration is enabled and the user agent has no white card to display it,
35
+ * Add a rendering context to the vary string data to distinguish which versions of the category PLP should be cached
36
+ * (If the " prevent backend rendering" configuration is enabled and the user agent is not whitelisted to display it,
39
37
* we set a different page variation, and the FPC stores a different cached page)
40
38
*
41
- * @param HttpContext $subject
42
- * @param string[] $data
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!
43
42
*
44
- * @return array
43
+ * @param HttpContext $subject
44
+ * @param array $data
45
+ * @return array original params
46
+ * @throws NoSuchEntityException
45
47
*/
46
- public function afterGetData (HttpContext $ subject , $ data )
47
- {
48
- $ storeId = $ this ->storeManager ->getStore ()->getId ();
49
- if (!($ this ->request ->getControllerName () === 'category '
50
- && $ this ->configHelper ->replaceCategories ($ storeId ) === true )) {
48
+ public function afterGetData (HttpContext $ subject , array $ data ): array {
49
+ if (!$ this ->shouldApplyCacheContext ()) {
51
50
return $ data ;
52
51
}
53
52
@@ -59,4 +58,52 @@ public function afterGetData(HttpContext $subject, $data)
59
58
60
59
return $ data ;
61
60
}
61
+
62
+ /**
63
+ * @param int $storeId
64
+ * @return string
65
+ */
66
+ protected function getOriginalRoute (int $ storeId ): string
67
+ {
68
+ $ requestUri = $ this ->request ->getRequestUri ();
69
+
70
+ $ rewrite = $ this ->urlFinder ->findOneByData ([
71
+ 'request_path ' => ltrim ($ requestUri , '/ ' ),
72
+ 'store_id ' => $ storeId ,
73
+ ]);
74
+
75
+ return $ rewrite ?->getTargetPath() ?? "" ;
76
+ }
77
+
78
+ /**
79
+ * @param string $path
80
+ * @return bool
81
+ */
82
+ protected function isCategoryRoute (string $ path ): bool {
83
+ $ path = ltrim ($ path , '/ ' );
84
+ return str_starts_with ($ path , self ::CATEGORY_ROUTE );
85
+ }
86
+
87
+ /**
88
+ * This can be called in a variety of contexts - so this should always be called consistently
89
+ *
90
+ * @param int $storeId
91
+ * @return bool
92
+ */
93
+ protected function isCategoryPage (int $ storeId ): bool
94
+ {
95
+ return $ this ->request ->getControllerName () === self ::CATEGORY_CONTROLLER
96
+ || $ this ->isCategoryRoute ($ this ->request ->getRequestUri ())
97
+ || $ this ->isCategoryRoute ($ this ->getOriginalRoute ($ storeId ));
98
+ }
99
+
100
+ /**
101
+ * @return bool
102
+ * @throws NoSuchEntityException
103
+ */
104
+ protected function shouldApplyCacheContext (): bool
105
+ {
106
+ $ storeId = $ this ->storeManager ->getStore ()->getId ();
107
+ return $ this ->isCategoryPage ($ storeId ) && $ this ->configHelper ->replaceCategories ($ storeId );
108
+ }
62
109
}
0 commit comments