Skip to content

Commit

Permalink
[BUGFIX] Read cache disable instruction from request where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Jan 26, 2025
1 parent 2049323 commit c4a1348
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Classes/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use FluidTYPO3\Vhs\Asset;
use FluidTYPO3\Vhs\Utility\CoreUtility;
use FluidTYPO3\Vhs\ViewHelpers\Asset\AssetInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\SingletonInterface;
Expand All @@ -20,6 +21,7 @@
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Frontend\Cache\CacheInstruction;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;

Expand Down Expand Up @@ -750,9 +752,6 @@ protected function getFileIntegrity(string $file): ?string
return null;
}

/** @var TypoScriptFrontendController $typoScriptFrontendController */
$typoScriptFrontendController = $GLOBALS['TSFE'];

$integrity = null;
$integrityMethod = ['sha256','sha384','sha512'][
$typoScript['assets.']['tagsAddSubresourceIntegrity'] - 1
Expand All @@ -766,8 +765,7 @@ protected function getFileIntegrity(string $file): ?string
if (!file_exists($integrityFile)
|| 0 === filemtime($integrityFile)
|| isset($GLOBALS['BE_USER'])
|| $typoScriptFrontendController->no_cache
|| $typoScriptFrontendController->page['no_cache']
|| $this->readCacheDisabledInstructionFromContext()
) {
if (extension_loaded('hash') && function_exists('hash_file')) {
$integrity = base64_encode((string) hash_file($integrityMethod, $file, true));
Expand Down Expand Up @@ -800,4 +798,23 @@ protected function resolveAbsolutePathForFile(string $filename): string
{
return GeneralUtility::getFileAbsFileName($filename);
}

protected function readCacheDisabledInstructionFromContext(): bool
{
$hasDisabledInstructionInRequest = false;

/** @var ServerRequestInterface $serverRequest */
$serverRequest = $GLOBALS['TYPO3_REQUEST'];
$instruction = $serverRequest->getAttribute('frontend.cache.instruction');
if ($instruction instanceof CacheInstruction) {
$hasDisabledInstructionInRequest = !$instruction->isCachingAllowed();
}

/** @var TypoScriptFrontendController $typoScriptFrontendController */
$typoScriptFrontendController = $GLOBALS['TSFE'];

return $hasDisabledInstructionInRequest
|| $typoScriptFrontendController->no_cache
|| (is_array($typoScriptFrontendController->page) && $typoScriptFrontendController->page['no_cache']);
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parameters:
- "#^Class TYPO3\\\\CMS\\\\Extbase\\\\Domain\\\\Repository\\\\FrontendUserRepository not found\\.$#"
- "#^Class TYPO3\\\\CMS\\\\Extbase\\\\Domain\\\\Model\\\\(Frontend|Backend)User(Group)? not found\\.$#"
- "#^Class TYPO3\\\\CMS\\\\Core\\\\Imaging\\\\ImageResource not found\\.$#"
- "#^Class TYPO3\\\\CMS\\\\Frontend\\\\Cache\\\\CacheInstruction not found\\.$#"
- "#(unknown class|invalid( return)? type) Doctrine\\\\DBAL\\\\Driver\\\\ResultStatement#"
- "#(unknown class|invalid( return)? type) TYPO3\\\\CMS\\\\Frontend\\\\Event\\\\AfterCacheableContentIsGeneratedEvent#"
- "#(unknown class|invalid( return)? type) TYPO3\\\\CMS\\\\Extbase\\\\Mvc\\\\Controller\\\\ControllerContext#"
Expand All @@ -43,6 +44,7 @@ parameters:
- "#unknown class TYPO3\\\\CMS\\\\Core\\\\Collection\\\\RecordCollectionRepository#"
- "#unknown class TYPO3\\\\CMS\\\\Extbase\\\\Mvc\\\\Web\\\\Request#"
- "#unknown class TYPO3\\\\CMS\\\\Extbase\\\\Mvc\\\\ResponseInterface#"
- "#unknown class TYPO3\\\\CMS\\\\Frontend\\\\Cache\\\\CacheInstruction#"
- "#invalid return type TYPO3\\\\CMS\\\\Frontend\\\\Page\\\\PageRepository#"
- "#invalid return type TYPO3\\\\CMS\\\\Extbase\\\\Mvc\\\\ResponseInterface#"
- "#invalid type TYPO3\\\\CMS\\\\Core\\\\Collection\\\\RecordCollectionRepository#"
Expand Down

0 comments on commit c4a1348

Please sign in to comment.