Solved

leverage browser caching

Posted on 2013-01-21
11
796 Views
Last Modified: 2013-01-23
Dear Experts,

I checked my web site on Google PageSpeed Insights and it reccomends that I should leverage browser caching?
How can I leverage browser caching?

I updated my web page weekly and I use below code on my page for my visitors to see the last updates..

<meta http-equiv="cache-control" content="no-cache" />
 
<meta http-equiv="pragma" content="no-cache" />
 
<meta http-equiv="expires" content="0" />
0
Comment
Question by:Braveheartli
  • 6
  • 4
11 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Actually, that is the opposite of what they are recommending.  "no-cache" is intended to force the browser to get content from the server every time.  To "leverage browser caching", you want the page to be cached as long as possible in the browser so it does not have to be fetched from the server.  PageSpeed looks at the headers returned from the server to see what expiration dates are requested.

If you are on shared hosting, you usually can't change the expiration times except maybe thru 'htaccess' on Apache.  I'm not sure what you would do on IIS.  In any case, to speed up browsing and lower the number of server accesses, you would want to remove those 'meta' statements from your pages.
0
 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
Dear DaveBaldwin
I have a dedicated server.
What happens if I remove the codes ?

<meta http-equiv="cache-control" content="no-cache" />
 
<meta http-equiv="pragma" content="no-cache" />
 
<meta http-equiv="expires" content="0" />

can my users see the last updates on my page if I remove the code ?
0
 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
Should I only use the cache code only the pages that I update frequently?
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
In general you need the no cache meta tags for pages that change frequently, or are dynamically generated with changed data.  The only pages you should remove the no cache meta tags on are pages that do not change on a regular basis.

If a browser has a page in cache, it will not even request if from the server, it will just use cache, so you need to have an expiry on it or it could end up in browser cash forever; depending on user settings.

Cd&
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
Caching of HTML documents isn't what matters the most so the 'meta' tags aren't that important.  This page http://tutorialpedia.org/tutorials/Apache+enable+file+caching+with+htaccess.html shows how to set cache and expiration times on the server for other types of files like images.  It is not unusual for image files to be much larger than the HTML page they are used in.  If the image does not change very often, it speeds up the page loading to load it from the browser cache but you have to request that on the server because an image file has no 'meta' tags.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
Dear DaveBaldwin

is it correct to write .htaccess file like this

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif)$">
  Header set Cache-Control "max-age=864000, public, must-revalidate"
  Header unset Last-Modified
</FilesMatch>
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
That looks like it should work. http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/

Have you tried it yet?
0
 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
No i haven't tried it because i don't know how to check if it's working or not.
Thank u
0
 
LVL 1

Author Comment

by:Braveheartli
Comment Utility
Redirection works but i dont know how to check image cache
0
 
LVL 1

Author Closing Comment

by:Braveheartli
Comment Utility
thank you
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Did you find a way to check the HTTP headers to make sure your cache expiration is being included?  In Firefox, I use LiveHTTP Headers or Fiddler.  You can also use Wireshark to check the headers.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now