Skip to main content

Leverage Browser Caching

Comments

9 comments

  • linux4me2
    I have been using cache-control rather than mod_expires on my server. I put the following in WHM > Service Configuration > Apache Configuration > Include Editor > Pre Virtual Host Include > All Versions: ## Cache-Control # 1 YEAR Header set Cache-Control "max-age=29030400, public"
    Note that I don't have CSS or JS in there; the changes made to those files are frequent enough that I don't want them cached. With this setup, Pagespeed lists Google Analytics as the only JS file under "Leverage browser caching" for some reason, even though there are a number of other JS files on the site(s). You might give it a try and see how it works for you with your Pagespeed results. The other advantage of this Pre Virtual Host include is that you don't have to edit every .htaccess, it works for all the domains on your server. While you're at it, you can add the following to enable Gzip compression, too: # Enable GZIP Compression. SetOutputFilter DEFLATE # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48 # the above regex won't work. You can use the following # workaround to get the desired effect: BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html # Don't compress images SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary
    0
  • fullfatdesigns
    Thanks linux4me2 I'll give that a go. Regards Wayne
    0
  • cPanelMichael
    Hello @fullfatdesigns, Let us know how it goes. Thank you.
    0
  • fullfatdesigns
    Hi. I've only just had a chance to try this. I've done the steps listed above and added ## Cache-Control # 1 YEAR Header set Cache-Control "max-age=29030400, public"
    which includes js and css, but pagespeed is still reporting css and js files are missing expiry headers.
    0
  • linux4me2
    Well, there is an "if" conditional statement to determine if mod_headers is present before the cache control will execute. I guess the first thing to do is to go WHM > Software > EasyApache4 and click on "View all packages" in the "Currently Installed Packages" section to make sure you have mod_headers installed. If mod_headers is installed, you can test your site using an online tool like GiftofSpeed. The latter lists all the cached and non-cached files on your site with the duration they're cached and how it is done, which should let you verify whether or not your Cache-Control setting is working.
    0
  • fullfatdesigns
    Hi linux4me2 Thanks for the reply. I've checked in EA4 and mod_headers is installed. I've checked using the links you provided. GiftofSpeed showed that there was a mixture of cached and non-cached js and css. I noticed the ones that aren't are external js and css, so I guess that would make sense and js and css on the server are cached. Does that sound right?
    0
  • linux4me2
    Hi linux4me2 Thanks for the reply. I've checked in EA4 and mod_headers is installed. I've checked using the links you provided. GiftofSpeed showed that there was a mixture of cached and non-cached js and css. I noticed the ones that aren't are external js and css, so I guess that would make sense and js and css on the server are cached. Does that sound right?

    I didn't realize external files would not be cached by the browser, but that makes sense. Here's an
    0
  • fullfatdesigns
    Thanks. Great article.
    0
  • vlee
    I have added HTTP Strict Transport Security (HSTS) which help with extra SSL security This sets the Strict-Transport-Security policy field parameter. It forces those connections over HTTPS encryption, disregarding any script's call to load any resource in that domain over HTTP. HSTS is but one arrow in a bundled sheaf of security settings for your web server or your web hosting service. ## HTTP Strict Transport Security Header set Strict-Transport-Security "max-age=15552000; includeSubDomains; preload" ## Cache-Control # 1 YEAR Header set Cache-Control "max-age=29030400, public"
    0

Please sign in to leave a comment.