7
7
use Magento \Framework \App \Request \Http ;
8
8
use Magento \Framework \Exception \NoSuchEntityException ;
9
9
use Magento \Store \Model \StoreManagerInterface ;
10
+ use Magento \UrlRewrite \Model \UrlFinderInterface ;
10
11
11
12
/**
12
13
* 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
23
24
public function __construct (
24
25
protected ConfigHelper $ configHelper ,
25
26
protected StoreManagerInterface $ storeManager ,
26
- protected Http $ request
27
+ protected Http $ request ,
28
+ protected UrlFinderInterface $ urlFinder
27
29
) { }
28
30
29
31
/**
@@ -37,7 +39,7 @@ public function __construct(
37
39
* @throws NoSuchEntityException
38
40
*/
39
41
public function beforeGetVaryString (HttpContext $ subject ): array {
40
- if (!$ this ->applyCacheContext ()) {
42
+ if (!$ this ->shouldApplyCacheContext ()) {
41
43
return [];
42
44
}
43
45
@@ -54,13 +56,39 @@ public function beforeGetVaryString(HttpContext $subject): array {
54
56
return [];
55
57
}
56
58
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
+
57
85
/**
58
86
* @return bool
59
87
* @throws NoSuchEntityException
60
88
*/
61
- protected function applyCacheContext (): bool
89
+ protected function shouldApplyCacheContext (): bool
62
90
{
63
91
$ storeId = $ this ->storeManager ->getStore ()->getId ();
64
- return $ this ->request -> getControllerName () == ' category ' && $ this ->configHelper ->replaceCategories ($ storeId );
92
+ return $ this ->isCategoryPage ( $ storeId ) && $ this ->configHelper ->replaceCategories ($ storeId );
65
93
}
66
94
}
0 commit comments