From fd75e137eec0e0189490039c1fbca369bfc15281 Mon Sep 17 00:00:00 2001 From: Patrick Broens Date: Thu, 12 Dec 2024 15:45:37 +0100 Subject: [PATCH] chore: Make changes for TYPO3 v12 --- Classes/Middleware/CacheHeaders.php | 16 +++++++++------- .../opengemeenten_cacheheaders.typoscript | 6 +++--- README.md | 4 ++++ composer.json | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Classes/Middleware/CacheHeaders.php b/Classes/Middleware/CacheHeaders.php index 50d0a43..0d9acd7 100644 --- a/Classes/Middleware/CacheHeaders.php +++ b/Classes/Middleware/CacheHeaders.php @@ -6,6 +6,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; +use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException; use TYPO3\CMS\Core\Http\NullResponse; use TYPO3\CMS\Core\Http\Response; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; @@ -41,9 +42,12 @@ class CacheHeaders implements MiddlewareInterface * - Return status '200 OK' when ETag has changed (content has changed) * - Send header 'cache-control: no-cache' * + * IMPORTANT NOTE: Keep in mind when using "nonce" the browser (and TYPO3) will disable client side caching. + * * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface + * @throws AspectNotFoundException */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { @@ -54,17 +58,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface && (bool)$GLOBALS['TSFE']->config['config']['OpenGemeenten\CmsFrontend.']['sendCacheHeaders'] === true && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController && $GLOBALS['TSFE']->isStaticCacheble() - && !$GLOBALS['TSFE']->isBackendUserLoggedIn() - && !$GLOBALS['TSFE']->doWorkspacePreview() - && ( - empty($GLOBALS['TSFE']->config['config']['sendCacheHeaders_onlyWhenLoginDeniedInBranch']) - || empty($GLOBALS['TSFE']->checkIfLoginAllowedInBranch()) - ) + && !$GLOBALS['TSFE']->getContext()->getPropertyFromAspect('backend.user', 'isLoggedIn', false) + && !$GLOBALS['TSFE']->getContext()->getPropertyFromAspect('workspace', 'isOffline', false) ) { $configuration = $GLOBALS['TSFE']->config['config']; // If cache headers are set within TYPO3, unset most of them - if ((bool)$configuration['sendCacheHeaders'] === true) { + if (isset($configuration['sendCacheHeaders']) + && (bool)$configuration['sendCacheHeaders'] === true + ) { $response = $response->withoutHeader('cache-control'); $response = $response->withoutHeader('expires'); $response = $response->withoutHeader('pragma'); diff --git a/Configuration/TypoScript/Constants/Extensions/opengemeenten_cacheheaders.typoscript b/Configuration/TypoScript/Constants/Extensions/opengemeenten_cacheheaders.typoscript index 4ecceb4..b81195c 100644 --- a/Configuration/TypoScript/Constants/Extensions/opengemeenten_cacheheaders.typoscript +++ b/Configuration/TypoScript/Constants/Extensions/opengemeenten_cacheheaders.typoscript @@ -1,7 +1,7 @@ extensions.opengemeenten_cacheheaders { cacheHeaders { - sendCacheHeaders = 0 - pattern = ^("[0-9a-fA-F]*)(\b-gzip\b|)(")$ - replacement = $1$3 + sendCacheHeaders = 1 + pattern = ^([W]\/|)(")([0-9a-fA-F]*)(\b-gzip\b|)(")$ + replacement = $3 } } diff --git a/README.md b/README.md index 7034fe7..73434ca 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,10 @@ The `ETag` header is a `md5` hash of the content of a page and will be calculate When the HTTP status code `304, Not Modified` is send, no content will be send, keeping the response very small. See`Symfony\Component\HttpFoundation` +## IMPORTANT NOTE + +When using CSP in TYPO3 with `nonce`, browsers (and TYPO3) will disable client-side caching. + ## More info You can find a great tutorial on Medium by Alex Barashkov: diff --git a/composer.json b/composer.json index d9b7747..ac25ba1 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "typo3/cms-frontend": "^10.4 || ^11.5" + "typo3/cms-frontend": "^12.4" }, "extra": { "typo3/cms": {