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

Topic Rating


08 Apr 2016 16:31 - 15 Sep 2016 22:36 #1 by Trony

  • Administrator
  • Administrator
  • Location: Roma
  • Time Zone: UTC +2:00
  • Local Time: 11:13

  • Posts: 939
  • Profile views: 3260
  • Thank you received: 8

  • Register Date: 01 Jul 2008
  • Last login: 22 Sep 2021 22:50
  • Speed Up Joomla 3.5 with Cache Plugin and 304 Not Modified Header was created by Trony


    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.

    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
    <?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 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');
    		}
    		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 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)
    	{
    		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

    Before


    After



    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 | Soundcloud
    Last edit: 15 Sep 2016 22:36 by Trony.

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

    Moderators: Trony

    You might also be interested...

    FusoElektronique - Pure Electronic Music.

    Community and Web Radio of Electronic Music since July 2008. Rome, Italy

    Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) 2021.

    About us | Our team | Cookie Policy | Privacy Policy | Terms and Conditions | Contact us | Sitemap