Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
sg_cookie_optin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TYPO3
sg_cookie_optin
Commits
184ab1ec
Commit
184ab1ec
authored
2 years ago
by
Georgi Mateev
Browse files
Options
Downloads
Patches
Plain Diff
[TASK]
#204
Remove cacheBuster get parameter and move it to the file name
parent
75bb0667
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!14
Release 4.7.0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Classes/Service/StaticFileGenerationService.php
+25
-3
25 additions, 3 deletions
Classes/Service/StaticFileGenerationService.php
Classes/UserFunction/AddCookieOptinJsAndCss.php
+30
-15
30 additions, 15 deletions
Classes/UserFunction/AddCookieOptinJsAndCss.php
with
55 additions
and
18 deletions
Classes/Service/StaticFileGenerationService.php
+
25
−
3
View file @
184ab1ec
...
@@ -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'
);
...
...
This diff is collapsed.
Click to expand it.
Classes/UserFunction/AddCookieOptinJsAndCss.php
+
30
−
15
View file @
184ab1ec
...
@@ -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
*
*
...
...
This diff is collapsed.
Click to expand it.
Georgi
@Mateev
mentioned in commit
50de345e
·
2 years ago
mentioned in commit
50de345e
mentioned in commit 50de345e952144782d82941fca686785461c6dcf
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment