diff --git a/Classes/Middleware/RequestCacheMiddleware.php b/Classes/Middleware/RequestCacheMiddleware.php index dd9c6dc..91469b6 100644 --- a/Classes/Middleware/RequestCacheMiddleware.php +++ b/Classes/Middleware/RequestCacheMiddleware.php @@ -46,12 +46,24 @@ class RequestCacheMiddleware implements MiddlewareInterface */ protected $ignoredQueryParams; + /** + * @var array + * @Flow\InjectConfiguration(path="request.queryParams.ignorePrefix") + */ + protected $ignoredQueryPrefix; + /** * @var array * @Flow\InjectConfiguration(path="request.cookieParams.ignore") */ protected $ignoredCookieParams; + /** + * @var array + * @Flow\InjectConfiguration(path="request.cookieParams.ignorePrefix") + */ + protected $ignoredCookiePrefix; + /** * @var boolean * @Flow\InjectConfiguration(path="maxPublicCacheTime") @@ -136,6 +148,9 @@ protected function getCacheIdentifierForRequestIfCacheable(ServerRequestInterfac case (in_array($key, $this->ignoredQueryParams)): $ignoredQueryParams[$key] = $value; break; + case (array_reduce($this->ignoredQueryPrefix, fn ($carry, $prefix) => $carry || strpos($key, $prefix) === 0, false)): + $ignoredQueryParams[$key] = $value; + break; default: $disallowedQueryParams[$key] = $value; } @@ -148,7 +163,8 @@ protected function getCacheIdentifierForRequestIfCacheable(ServerRequestInterfac $requestCookieParams = $request->getCookieParams(); $disallowedCookieParams = []; foreach ($requestCookieParams as $key => $value) { - if (!in_array($key, $this->ignoredCookieParams)) { + $prefixed = array_reduce($this->ignoredCookiePrefix, fn ($carry, $prefix) => $carry || strpos($key, $prefix) === 0, false); + if (!in_array($key, $this->ignoredCookieParams) && !$prefixed) { $disallowedCookieParams[$key] = $value; } } diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 641dcfe..b17add1 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -19,6 +19,11 @@ Flowpack: # if they are only used on the client side ignore: [] + # ignored cookie params based on prefix exclude cookies that are handled by the frontend + # and are not relevant for the backend. A usecase would be affiliate cookies which are + # dynamically generated if they are only used on the client side + ignorePrefix: [] + # a request will only qualify for caching if it only contains queryParams that # are allowed or ignored. All other arguments will prevent caching. queryParams: @@ -31,6 +36,11 @@ Flowpack: # the backend like utm_campaign ignore: [] + # ignored prefix arguments are not part of the cache identifier but do not + # prevent caching either. Use this for arguments that are meaningless for + # the backend like utm_ + ignorePrefix: [] + Neos: Flow: http: diff --git a/README.md b/README.md index f30a118..732f794 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,11 @@ Flowpack: # and are not relevant for the backend. A usecase would be gdpr consent cookies # if they are only used on the client side ignore: [] + + # ignored cookie params based on prefix exclude cookies that are handled by the frontend + # and are not relevant for the backend. A usecase would be affiliate cookies which are + # dynamically generated if they are only used on the client side + ignorePrefix: [] # a request will only qualify for caching if it only contains queryParams that # are allowed or ignored. All other arguments will prevent caching. @@ -43,6 +48,11 @@ Flowpack: # prevent caching either. Use this for arguments that are meaningless for # the backend like utm_campaign ignore: [] + + # ignored prefix arguments are not part of the cache identifier but do not + # prevent caching either. Use this for arguments that are meaningless for + # the backend like utm_ + ignorePrefix: [] ``` You can also move the cache backend to something faster if available, to improve performance even more.