304 Not Modified (RFC 7232)
Indicates that the resource has not been modified since the version specified by the request headers If-Modified-Since or If-None-Match. In such case, there is no need to retransmit the resource since the client still has a previously-downloaded copy.
With this Hack, we tell to the browser (users) and spiderbot that resource isn't changed so "you don't have to download it again".
In this way we save Bandwidth (very important for Mobile Devices) and of course we Speed Up the Loading of the Joomla.
WARNING: It's a Joomla Hack Core. BACKUP ALWAYS AND FOREVER !!!
First Step
Enable Default Cache Plugin
Second Step
Open your
index.php of your
template and put this code at the top
Code:
<?php
jimport('joomla.environment.response');
JResponse::allowCache(true);
$time = time()- 1;
$etagFuso = md5($_SERVER['REQUEST_URI']);
JResponse::setHeader('ETag', 'W/"'.$etagFuso.'"', true );
JResponse::setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $time) . ' GMT', true);
$user =& JFactory::getUser();
if ($user->get('guest') == 1) {
JResponse::setHeader('Expires', gmdate('D, d M Y H:i:s', time()+ 900) . ' GMT');
JResponse::setHeader('Cache-Control', 'private,s-maxage=900,max-age=900,must-revalidate', false );
} else {
JResponse::setHeader('Expires', 'Wed, 17 Aug 2005 00:00:00 GMT', true);
JResponse::setHeader('Cache-Control', 'no-store,no-cache,must-revalidate,post-check=0,pre-check=0,max-age=0', false );
}
JResponse::setHeader('Pragma', 'no-cache', true );
?>
Third Step
Open
web.php in
libraries ►
joomla ►
application
and comment with // this part of the code:
Code:
// If the response is set to uncachable, we need to set some appropriate headers so browsers don't cache the response.
if (!$this->response->cachable)
{
// Expires in the past.
//$this->setHeader('Expires', 'Wed, 17 Aug 2005 00:00:00 GMT', true);
// Always modified.
// $this->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT', true);
// $this->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false);
// HTTP 1.0
$this->setHeader('Pragma', 'no-cache');
}
else
{
// Expires.
// $this->setHeader('Expires', gmdate('D, d M Y H:i:s', time() + 900) . ' GMT');
// Last modified.
if ($this->modifiedDate instanceof JDate)
{
$this->setHeader('Last-Modified', $this->modifiedDate->format('D, d M Y H:i:s'));
}
}
Fourth Step
Open
page.php in
libraries ►
cache ►
controller
and at the end:
Code:
/**
* Set the ETag header in the response
*
* @param string $etag The entity tag (etag) to set
*
* @return void
*
* @since 11.1
*/
protected function _setEtag($etag)
{
jimport('joomla.environment.response');
JResponse::allowCache(true);
$etagFuso = md5($id);
$last_modified = filemtime($_SERVER['REQUEST_URI']);
if(!headers_sent() && isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) || !headers_sent() && isset($_SERVER['HTTP_IF_NONE_MATCH']))
{
if(strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) >= $last_modified || stripslashes($_SERVER['HTTP_IF_NONE_MATCH']))
{
header('HTTP/1.x 304 Not Modified', true);
header('ETag: '. 'W/"'.$etagFuso.'"', true );
header('Expires: '. gmdate('D, d M Y H:i:s', time()+ 900) . ' GMT');
header('Cache-Control: private,s-maxage=900,max-age=900,must-revalidate', false );
header('Last-Modified: '. gmdate("D, d M Y H:i:s", $last_modified) . ' GMT');
header('Pragma: no-cache', false );
exit();
}
}
}
}
For to Validate the 304 Headers, I set Expires and Cache Control for 900s (15min) as Default Time of Joomla Cache.
Result
You can see how:
Size: from 24.4KB to 161bytes
Requests: No Download Again our Page
Transferred: from 51.3KB / 74.5KB to 27.0KB / 55.2KB
DOMContentLoaded: from 5.02s | 5.32s to 1.73s | 1.85s
(DOMContentLoaded time is high because I had just emptied the cache and so I overloaded my hosting)
This Hack work Only for No Logged Users and Cached Pages by Joomla and with Time Cache in Global Configuration