Forum Moderators: phranque
<link rel="prefetch" href="/path/to/css.css"> if ($_COOKIE['css_exists'])
echo <<<EOF
<link rel="stylesheet" href="/path/to/css.css">
EOF;
else {
echo <<<EOF
<style>
EOF;
include "/path/to/css.css";
echo <<<EOF
</style>
<link rel="prefetch" href="/path/to/css.css">
EOF;
setcookie('css_exists', '1', 365, '/');
} <script>
$.get('/path/to/css.css');
</script>
<?php
echo <<<EOF
<head>
...
EOF;
if (!$_COOKIE['css_exists'])
include '/path/to/css.css';
echo <<<EOF
<script>
// common setCookie() function, I think
function setCookie(c_name, value, expiredays) {
if (!expiredays) expiredays = -1;
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + '=' + escape(value) + '; path=/' + ((expiredays == null) ? '' : '; expires=' + exdate.toGMTString());
}
</script>
</head>
<body
...
<script>
$(function() {
$.get('/path/to/css.css', function() {
setCookie('css_exists', '1', 365);
});
});
</script>
</body>
</html>
EOF;
?> # plugged in by cPanel while gzipping
<IfModule mod_deflate.c>
AddOutPutFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/x-javascript text/xml application/xml application/xml+rss application/vnd.ms-fontobject application/x-font-ttf
</IfModule> <FilesMatch "\.(ico|css|js|jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch> In the top menu of the network tab to the left of the red "record" icon there are various check boxes, there is one for "disable cache" is it checked?
To see the header info, click on the "name" eg: test.php and the view will change, you should see to the right "Headers", "Preview" "Response" etc... The header info will be in the "Headers" tab.
Request Method: GET
Status Code: 200 OK
Referrer Policy: no-referrer-when-downgrade
Accept-Ranges: bytes
Cache-Control: max-age=31536000, public
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 9267
Content-Type: text/css
Date: Thu, 20 Feb 2020 18:20:32 GMT
Keep-Alive: timeout=5, max=20
Last-Modified: Wed, 19 Feb 2020 19:10:08 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Accept: text/css,*/*;q=0.1
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Pragma: no-cache
Sec-Fetch-Dest: style
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: same-origin Cache-Control: no-cache
Pragma: no-cache ; Set to {nocache,private,public,} to determine HTTP caching aspects
; or leave this empty to avoid sending anti-caching headers.
session.cache_limiter = nocache
; Document expires after n minutes.
session.cache_expire = 180 echo <<<EOF
<link rel="prefetch" href="/path/to/css.css">
EOF;
if (!$_COOKIE['css_exists']) {
$style = file_get_contents('/path/to/css.css');
if ($style) {
// homemade minimizer, removed comments and unnecessary spaces
$pattern = '#/\*[^*]*\*+([^/][^*]*\*+)* /|\r\n|\r|\n|\t#';
$style = preg_replace($pattern, ' ', $style);
$style = preg_replace('#\s{2,}#', ' ', $style);
$style = preg_replace('#^\s|\s$|\s?([=),;+])\s?#', '$1', $style);
echo <<<EOF
<style>
$style
</style>
EOF;
setcookie('css_exists', '1', 365, '/');
}
}
if ($_COOKIE['css_exists'] || !$style)
echo <<<EOF
<link rel="stylesheet" href="/path/to/css.css">
...
EOF;
if (!$_COOKIE['css_exists']) {
$style = file_get_contents('/path/to/css.css');
if ($style) {
// homemade minimizer, removed comments and unnecessary spaces
$pattern = '#/\*[^*]*\*+([^/][^*]*\*+)* /|\r\n|\r|\n|\t#';
$style = preg_replace($pattern, ' ', $style);
$style = preg_replace('#\s{2,}#', ' ', $style);
$style = preg_replace('#^\s|\s$|\s?([=),;+])\s?#', '$1', $style);
echo <<<EOF
<style>
$style
</style>
EOF;
}
}
if ($_COOKIE['css_exists'] || !$style)
echo <<<EOF
<link rel="stylesheet" href="/path/to/css.css">
...
<script>
$(function() {
$.get('/path/to/css.css', function() {
setCookie('css_exists', '1', 365);
});
});
</script>
</body>
</html>
EOF;
So if anyone can confirm whether the callback function loads after css.css is fully loaded, then that would be the better way to go.
// at the bottom of the main page
<script>
$.get('https://www.mysite.com/test.js', function() {
console.log('made it to B');
});
</script>
// test.js
// I chose a high number to make sure it took some time to load, but it turns out that I could have
// just used 1000 and been sure
for (var i = 0; i < 100000; i++)
console.log(i); I THINK that I've been able to confirm that the callback DOES run after the $.get() file has loaded.
// homemade minimizer, removed comments and unnecessary spaces
As noted in the jQuery docs [api.jquery.com], it is a "callback function that is executed if the request succeeds".
I would suggest pre-optimizing the CSS file, since those preg_replace() calls are expensive.
function no_comments($tokens) {
$remove = [];
$suspects = ['T_COMMENT', 'T_DOC_COMMENT'];
$iterate = token_get_all('<?php ' . PHP_EOL . $tokens);
foreach ($iterate as $token) {
if (is_array($token)) {
$name = token_name($token[0]);
$chr = substr($token[1], 0, 1);
if (in_array ($name, $suspects) && $chr !== '#')
$remove[] = $token[1];
}
}
return str_replace($remove, null, $tokens);
}
no_comments($style);