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 6a88ab7
Showing 1 changed file with 22 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) {

Check failure on line 809 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 7.4 TYPO3 ^10.4

Class TYPO3\CMS\Frontend\Cache\CacheInstruction not found.

Check failure on line 809 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.0 TYPO3 ^11.5

Class TYPO3\CMS\Frontend\Cache\CacheInstruction not found.

Check failure on line 809 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.1 TYPO3 ^11.5

Class TYPO3\CMS\Frontend\Cache\CacheInstruction not found.

Check failure on line 809 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.1 TYPO3 ^12.4

Class TYPO3\CMS\Frontend\Cache\CacheInstruction not found.

Check failure on line 809 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.2 TYPO3 ^12.4

Class TYPO3\CMS\Frontend\Cache\CacheInstruction not found.

Check failure on line 809 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.3 TYPO3 ^12.4

Class TYPO3\CMS\Frontend\Cache\CacheInstruction not found.
$hasDisabledInstructionInRequest = !$instruction->isCachingAllowed();

Check failure on line 810 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 7.4 TYPO3 ^10.4

Call to method isCachingAllowed() on an unknown class TYPO3\CMS\Frontend\Cache\CacheInstruction.

Check failure on line 810 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.0 TYPO3 ^11.5

Call to method isCachingAllowed() on an unknown class TYPO3\CMS\Frontend\Cache\CacheInstruction.

Check failure on line 810 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.1 TYPO3 ^11.5

Call to method isCachingAllowed() on an unknown class TYPO3\CMS\Frontend\Cache\CacheInstruction.

Check failure on line 810 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.1 TYPO3 ^12.4

Call to method isCachingAllowed() on an unknown class TYPO3\CMS\Frontend\Cache\CacheInstruction.

Check failure on line 810 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.2 TYPO3 ^12.4

Call to method isCachingAllowed() on an unknown class TYPO3\CMS\Frontend\Cache\CacheInstruction.

Check failure on line 810 in Classes/Service/AssetService.php

View workflow job for this annotation

GitHub Actions / PHPStan, PHP 8.3 TYPO3 ^12.4

Call to method isCachingAllowed() on an unknown class TYPO3\CMS\Frontend\Cache\CacheInstruction.
}

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

return $hasDisabledInstructionInRequest
|| $typoScriptFrontendController->no_cache
|| (is_array($typoScriptFrontendController->page) && $typoScriptFrontendController->page['no_cache']);
}
}

0 comments on commit 6a88ab7

Please sign in to comment.