file Speed Up Joomla 3.5 with Cache Plugin and 304 Not Modified Header

08 Apr 2016 16:31 - 15 Sep 2016 22:36 #1 by Trony
Trony created the topic: Speed Up Joomla 3.5 with Cache Plugin and 304 Not Modified Header

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.

Source: Wikipedia

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.


First Step
Enable Default Cache Plugin

Second Step
Open your index.php of your template and put this code at the top
$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 librariesjoomlaapplication
and comment with // this part of the 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');
			// 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 librariescachecontroller
and at the end:
	 * 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)
		$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 );
For to Validate the 304 Headers, I set Expires and Cache Control for 900s (15min) as Default Time of Joomla Cache.



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
Music and Electronic Devices Designer. Drum and Bass Addict. Creative Mind as Lifestyle. Cat in past Life. Soccer, Ice Hockey and Snowboard Lover. Marxist.
Sono Il Re sul Carro Danzante. Attraversando La Città Getto Via I Coriandoli Della Vostra Tristezza Regalando Passione Ed Estasi Nei Vostri Occhi.
Follow Trony on: FusoElektronique | Facebook | Twitter | Google+ | Soundcloud


Last Edit: 15 Sep 2016 22:36 by Trony.

Please Log in or Create an account to join the conversation.

Web Hosting

Please. Leave a Vote at this Topic.

Moderators: Trony
【Forum】Speed Up Joomla 3.5 with Cache Plugin and 304 Not Modified Header ~ FusoElektronique Page 1 | Speed Up Joomla 3.5 with Cache Plugin and 304 Not Modified Header. Published by Trony on Friday, 08 April 2016 in Computers - WebMastering - Consolle (Design's Factory)
Time to create page: 0.109 seconds