From 8db7156dabeacd9b21fe5cb228d037699c0513e7 Mon Sep 17 00:00:00 2001 From: Lukas Walter Date: Thu, 18 Jul 2024 13:53:37 +0200 Subject: [PATCH 1/4] Add middleware to add 'frontend.cache.instructions' and minor bugfixes --- Classes/Cache/Rule/NoNoCache.php | 2 +- .../Middleware/FrontendCacheMiddleware.php | 33 +++++++++++++++++++ Configuration/RequestMiddlewares.php | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 Classes/Middleware/FrontendCacheMiddleware.php diff --git a/Classes/Cache/Rule/NoNoCache.php b/Classes/Cache/Rule/NoNoCache.php index 9665d526f0d..400563d21e3 100644 --- a/Classes/Cache/Rule/NoNoCache.php +++ b/Classes/Cache/Rule/NoNoCache.php @@ -19,7 +19,7 @@ public function checkRule(ServerRequestInterface $request, array &$explanation, { $tsfe = $GLOBALS['TSFE'] ?? null; /* @phpstan-ignore-next-line */ - if ($tsfe instanceof TypoScriptFrontendController && $tsfe->no_cache) { + if ($tsfe instanceof TypoScriptFrontendController && $tsfe->config['config']['no_cache']) { $explanation[__CLASS__] = 'config.no_cache is true'; } } diff --git a/Classes/Middleware/FrontendCacheMiddleware.php b/Classes/Middleware/FrontendCacheMiddleware.php new file mode 100644 index 00000000000..113b5a2ee15 --- /dev/null +++ b/Classes/Middleware/FrontendCacheMiddleware.php @@ -0,0 +1,33 @@ +getAttribute( + 'frontend.cache.instruction', + new CacheInstruction(), + ); + + // Disable the cache and give a reason + $cacheInstruction->disableCache('EXT:staticfilecache: Cache is disabled'); + + // Write back the cache instruction to the attribute + $request = $request->withAttribute('frontend.cache.instruction', $cacheInstruction); + + return $handler->handle($request); + } +} diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php index b0c84b13550..80779091c92 100644 --- a/Configuration/RequestMiddlewares.php +++ b/Configuration/RequestMiddlewares.php @@ -5,6 +5,7 @@ use SFC\Staticfilecache\Middleware\CookieCheckMiddleware; use SFC\Staticfilecache\Middleware\FallbackMiddleware; use SFC\Staticfilecache\Middleware\FrontendUserMiddleware; +use SFC\Staticfilecache\Middleware\FrontendCacheMiddleware; use SFC\Staticfilecache\Middleware\GenerateMiddleware; use SFC\Staticfilecache\Middleware\PrepareMiddleware; From c874afd4bfdf64faff093c38e7b5611351e715c6 Mon Sep 17 00:00:00 2001 From: Lukas Walter Date: Thu, 18 Jul 2024 13:56:26 +0200 Subject: [PATCH 2/4] Add missing reference in RequestMiddleware for FrontendCacheMiddleware --- Configuration/RequestMiddlewares.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php index 80779091c92..a36e2b1664a 100644 --- a/Configuration/RequestMiddlewares.php +++ b/Configuration/RequestMiddlewares.php @@ -41,6 +41,15 @@ 'staticfilecache/generate', ], ], + 'staticfilecache/frontend-cache' => [ + 'target' => FrontendCacheMiddleware::class, + 'after' => [ + 'typo3/cms-frontend/authentication', + ], + 'before' => [ + 'staticfilecache/generate', + ], + ], 'staticfilecache/cookie-check' => [ 'target' => CookieCheckMiddleware::class, 'before' => [ From f6e767d5a0af4b842884b5ab99273569a85b189b Mon Sep 17 00:00:00 2001 From: Lukas Walter Date: Fri, 2 Aug 2024 19:42:45 +0200 Subject: [PATCH 3/4] Revert change in NoNoCache after testing in local environment --- Classes/Cache/Rule/NoNoCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Cache/Rule/NoNoCache.php b/Classes/Cache/Rule/NoNoCache.php index 400563d21e3..9665d526f0d 100644 --- a/Classes/Cache/Rule/NoNoCache.php +++ b/Classes/Cache/Rule/NoNoCache.php @@ -19,7 +19,7 @@ public function checkRule(ServerRequestInterface $request, array &$explanation, { $tsfe = $GLOBALS['TSFE'] ?? null; /* @phpstan-ignore-next-line */ - if ($tsfe instanceof TypoScriptFrontendController && $tsfe->config['config']['no_cache']) { + if ($tsfe instanceof TypoScriptFrontendController && $tsfe->no_cache) { $explanation[__CLASS__] = 'config.no_cache is true'; } } From 7877202b20bde6b25341d3c1830273f3f3f15ab2 Mon Sep 17 00:00:00 2001 From: Lukas Walter Date: Thu, 15 Aug 2024 11:08:00 +0200 Subject: [PATCH 4/4] Add condition to check of CacheInstruction class exists, otherwise use 'fronted.chache.instruction' attribute --- .../Middleware/FrontendCacheMiddleware.php | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/Classes/Middleware/FrontendCacheMiddleware.php b/Classes/Middleware/FrontendCacheMiddleware.php index 113b5a2ee15..9a8340b61db 100644 --- a/Classes/Middleware/FrontendCacheMiddleware.php +++ b/Classes/Middleware/FrontendCacheMiddleware.php @@ -13,21 +13,23 @@ class FrontendCacheMiddleware implements MiddlewareInterface { public function process( - ServerRequestInterface $request, + ServerRequestInterface $request, RequestHandlerInterface $handler, - ): ResponseInterface { - // Get the attribute, if not available, use a new CacheInstruction object - $cacheInstruction = $request->getAttribute( - 'frontend.cache.instruction', - new CacheInstruction(), - ); + ): ResponseInterface + { + if (class_exists(CacheInstruction::class)) { + // Get the attribute, if not available, use a new CacheInstruction object + $cacheInstruction = $request->getAttribute( + 'frontend.cache.instruction', + new CacheInstruction(), + ); - // Disable the cache and give a reason - $cacheInstruction->disableCache('EXT:staticfilecache: Cache is disabled'); - - // Write back the cache instruction to the attribute - $request = $request->withAttribute('frontend.cache.instruction', $cacheInstruction); + // Disable the cache and give a reason + $cacheInstruction->disableCache('EXT:staticfilecache: Cache is disabled'); + // Write back the cache instruction to the attribute + $request = $request->withAttribute('frontend.cache.instruction', $cacheInstruction); + } return $handler->handle($request); } }