Skip to content
Snippets Groups Projects
Commit 184ab1ec authored by Georgi Mateev's avatar Georgi Mateev
Browse files

[TASK] #204 Remove cacheBuster get parameter and move it to the file name

parent 75bb0667
No related branches found
No related tags found
1 merge request!14Release 4.7.0
...@@ -65,6 +65,11 @@ class StaticFileGenerationService implements SingletonInterface { ...@@ -65,6 +65,11 @@ class StaticFileGenerationService implements SingletonInterface {
/** @var int */ /** @var int */
protected $siteRoot; protected $siteRoot;
/**
* @var string
*/
protected $cacheBusterString = '';
/** /**
* Generates the JavaScript, JSON and CSS files for this site root * Generates the JavaScript, JSON and CSS files for this site root
* *
...@@ -431,6 +436,7 @@ class StaticFileGenerationService implements SingletonInterface { ...@@ -431,6 +436,7 @@ class StaticFileGenerationService implements SingletonInterface {
*/ */
protected function createCSSFile(array $data, $folder, array $cssData, $minifyFile = TRUE) { protected function createCSSFile(array $data, $folder, array $cssData, $minifyFile = TRUE) {
$sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/'; $sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/';
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '11.0.0', '>')) { if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '11.0.0', '>')) {
$resourceFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class); $resourceFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
$file = $resourceFactory->retrieveFileOrFolderObject(self::TEMPLATE_STYLE_SHEET_PATH_EXT . self::TEMPLATE_STYLE_SHEET_NAME); $file = $resourceFactory->retrieveFileOrFolderObject(self::TEMPLATE_STYLE_SHEET_PATH_EXT . self::TEMPLATE_STYLE_SHEET_NAME);
...@@ -480,7 +486,7 @@ class StaticFileGenerationService implements SingletonInterface { ...@@ -480,7 +486,7 @@ class StaticFileGenerationService implements SingletonInterface {
$data[] = $value; $data[] = $value;
} }
$file = $sitePath . $folder . self::TEMPLATE_STYLE_SHEET_NAME; $file = $sitePath . $folder . str_replace('.css', $this->getCacheBusterString() . '.css', self::TEMPLATE_STYLE_SHEET_NAME);
file_put_contents($file, str_replace($keys, $data, $content)); file_put_contents($file, str_replace($keys, $data, $content));
if ($minifyFile) { if ($minifyFile) {
...@@ -512,6 +518,18 @@ class StaticFileGenerationService implements SingletonInterface { ...@@ -512,6 +518,18 @@ class StaticFileGenerationService implements SingletonInterface {
return $content; return $content;
} }
/**
* Generates a small cache buster string for the file names
* @return string
*/
protected function getCacheBusterString(): string {
if (!$this->cacheBusterString) {
$this->cacheBusterString = (string) substr(md5(microtime()), 0, 8);
}
return $this->cacheBusterString;
}
/** /**
* Creates a javascript file out of the given scripts array. * Creates a javascript file out of the given scripts array.
* *
...@@ -546,7 +564,7 @@ class StaticFileGenerationService implements SingletonInterface { ...@@ -546,7 +564,7 @@ class StaticFileGenerationService implements SingletonInterface {
return ''; return '';
} }
$file = $folder . $groupName . '-' . $languageUid . '.js'; $file = $folder . $groupName . $this->getCacheBusterString() . '-' . $languageUid . '.js';
$sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/'; $sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/';
$groupFile = $sitePath . $file; $groupFile = $sitePath . $file;
file_put_contents($groupFile, $content); file_put_contents($groupFile, $content);
...@@ -569,10 +587,12 @@ class StaticFileGenerationService implements SingletonInterface { ...@@ -569,10 +587,12 @@ class StaticFileGenerationService implements SingletonInterface {
* @param bool $minifyFile * @param bool $minifyFile
* *
* @return void * @return void
* @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
*/ */
protected function createJavaScriptFile($folder, $minifyFile = TRUE) { protected function createJavaScriptFile($folder, $minifyFile = TRUE) {
$sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/'; $sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/';
$file = $sitePath . $folder . self::TEMPLATE_JAVA_SCRIPT_NAME;
$file = $sitePath . $folder . str_replace('.js', $this->getCacheBusterString() . '.js', self::TEMPLATE_JAVA_SCRIPT_NAME);
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '11.0.0', '>')) { if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '11.0.0', '>')) {
$resourceFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class); $resourceFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
...@@ -1004,6 +1024,8 @@ class StaticFileGenerationService implements SingletonInterface { ...@@ -1004,6 +1024,8 @@ class StaticFileGenerationService implements SingletonInterface {
self::TEMPLATE_JSON_NAME self::TEMPLATE_JSON_NAME
); );
$file = str_replace('.json', $this->getCacheBusterString() . '.json', $file);
$mask = JSON_PRETTY_PRINT; $mask = JSON_PRETTY_PRINT;
if (defined('JSON_THROW_ON_ERROR')) { if (defined('JSON_THROW_ON_ERROR')) {
$mask = constant('JSON_THROW_ON_ERROR') | JSON_PRETTY_PRINT | constant('JSON_INVALID_UTF8_SUBSTITUTE'); $mask = constant('JSON_THROW_ON_ERROR') | JSON_PRETTY_PRINT | constant('JSON_INVALID_UTF8_SUBSTITUTE');
......
...@@ -78,18 +78,13 @@ class AddCookieOptinJsAndCss implements SingletonInterface { ...@@ -78,18 +78,13 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
$siteBaseUrl = BaseUrlService::getSiteBaseUrl($this->rootpage); $siteBaseUrl = BaseUrlService::getSiteBaseUrl($this->rootpage);
$file = $folder . 'siteroot-' . $rootPageId . '/' . 'cookieOptin.js';
$sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/'; $sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/';
$file = $this->getAssetFilePath($sitePath, $folder, $rootPageId, 'cookieOptin*.js');
$jsonFile = $this->getJsonFilePath($folder, $rootPageId, $sitePath); $jsonFile = $this->getJsonFilePath($folder, $rootPageId, $sitePath);
if ($jsonFile === NULL) { if ($jsonFile === NULL) {
return ''; return '';
} }
$cacheBuster = filemtime($sitePath . $file);
if (!$cacheBuster) {
$cacheBuster = '';
}
// we decode and encode again to remove the PRETTY_PRINT when rendering for better performance on the frontend // we decode and encode again to remove the PRETTY_PRINT when rendering for better performance on the frontend
// for easier debugging, you can check the generated file in the fileadmin // for easier debugging, you can check the generated file in the fileadmin
// see https://gitlab.sgalinski.de/typo3/sg_cookie_optin/-/issues/118 // see https://gitlab.sgalinski.de/typo3/sg_cookie_optin/-/issues/118
...@@ -105,7 +100,7 @@ class AddCookieOptinJsAndCss implements SingletonInterface { ...@@ -105,7 +100,7 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
$overwrittenBaseUrl = $jsonData['settings']['overwrite_baseurl']; $overwrittenBaseUrl = $jsonData['settings']['overwrite_baseurl'];
} }
$fileUrl = $overwrittenBaseUrl ?? $siteBaseUrl . $file . '?' . $cacheBuster; $fileUrl = $overwrittenBaseUrl ?? $siteBaseUrl . $file;
return '<script id="cookieOptinData" type="application/json">' . json_encode($jsonData) . '</script> return '<script id="cookieOptinData" type="application/json">' . json_encode($jsonData) . '</script>
<link rel="preload" as="script" href="' . $fileUrl . '" data-ignore="1" crossorigin="anonymous"> <link rel="preload" as="script" href="' . $fileUrl . '" data-ignore="1" crossorigin="anonymous">
<script src="' . $fileUrl . '" data-ignore="1" crossorigin="anonymous"></script>'; <script src="' . $fileUrl . '" data-ignore="1" crossorigin="anonymous"></script>';
...@@ -134,17 +129,12 @@ class AddCookieOptinJsAndCss implements SingletonInterface { ...@@ -134,17 +129,12 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
return ''; return '';
} }
$file = $folder . 'siteroot-' . $rootPageId . '/cookieOptin.css';
$sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/'; $sitePath = defined('PATH_site') ? PATH_site : Environment::getPublicPath() . '/';
$file = $this->getAssetFilePath($sitePath, $folder, $rootPageId, 'cookieOptin*.css');
if (!file_exists($sitePath . $file)) { if (!file_exists($sitePath . $file)) {
return ''; return '';
} }
$cacheBuster = filemtime($sitePath . $file);
if (!$cacheBuster) {
$cacheBuster = '';
}
$jsonFile = $this->getJsonFilePath($folder, $rootPageId, $sitePath); $jsonFile = $this->getJsonFilePath($folder, $rootPageId, $sitePath);
if ($jsonFile) { if ($jsonFile) {
$jsonData = json_decode(file_get_contents($sitePath . $jsonFile), TRUE); $jsonData = json_decode(file_get_contents($sitePath . $jsonFile), TRUE);
...@@ -159,8 +149,8 @@ class AddCookieOptinJsAndCss implements SingletonInterface { ...@@ -159,8 +149,8 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
} }
$siteBaseUrl = $overwrittenBaseUrl ?? BaseUrlService::getSiteBaseUrl($this->rootpage); $siteBaseUrl = $overwrittenBaseUrl ?? BaseUrlService::getSiteBaseUrl($this->rootpage);
return '<link rel="preload" as="style" href="' . $siteBaseUrl . $file . '?' . $cacheBuster . '" media="all" crossorigin="anonymous">' . "\n" return '<link rel="preload" as="style" href="' . $siteBaseUrl . $file . '" media="all" crossorigin="anonymous">' . "\n"
. '<link rel="stylesheet" href="' . $siteBaseUrl . $file . '?' . $cacheBuster . '" media="all" crossorigin="anonymous">' . "\n"; . '<link rel="stylesheet" href="' . $siteBaseUrl . $file . '" media="all" crossorigin="anonymous">' . "\n";
} }
/** /**
...@@ -205,6 +195,13 @@ class AddCookieOptinJsAndCss implements SingletonInterface { ...@@ -205,6 +195,13 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
* @throws SiteNotFoundException * @throws SiteNotFoundException
*/ */
protected function getJsonFilePath(string $folder, int $rootPageId, string $sitePath) { protected function getJsonFilePath(string $folder, int $rootPageId, string $sitePath) {
$jsonFiles = glob($sitePath . $folder . 'siteroot-' . $rootPageId . '/' . 'cookieOptinData' .
JsonImportService::LOCALE_SEPARATOR . $this->getLanguageWithLocale() . '*.json');
if (count($jsonFiles) > 0) {
// return the first file we found
return str_replace($sitePath, '', $jsonFiles[0]);
}
$jsonFile = $folder . 'siteroot-' . $rootPageId . '/' . 'cookieOptinData' . JsonImportService::LOCALE_SEPARATOR . $jsonFile = $folder . 'siteroot-' . $rootPageId . '/' . 'cookieOptinData' . JsonImportService::LOCALE_SEPARATOR .
$this->getLanguageWithLocale() . '.json'; $this->getLanguageWithLocale() . '.json';
if (!file_exists($sitePath . $jsonFile)) { if (!file_exists($sitePath . $jsonFile)) {
...@@ -221,6 +218,24 @@ class AddCookieOptinJsAndCss implements SingletonInterface { ...@@ -221,6 +218,24 @@ class AddCookieOptinJsAndCss implements SingletonInterface {
return $jsonFile; return $jsonFile;
} }
/**
* Get the current CSS or JS asset file path
*
* @param $sitePath
* @param $folder
* @param $rootPageId
* @param $pattern
* @return mixed|null
*/
protected function getAssetFilePath($sitePath, $folder, $rootPageId, $pattern) {
$files = glob($sitePath . $folder . 'siteroot-' . $rootPageId . '/' . $pattern);
if (count($files) > 0) {
return str_replace($sitePath, '', $files[0]);
}
return NULL;
}
/** /**
* Returns the current language id * Returns the current language id
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment