I faced browser caching issue for my website from last 4-5 days. I had a lot of research to resolve it but my website always loaded from browser cache. It was not loading from server until I refreshed it. If I refreshed URL, it loaded from server with my updated changes. After spending 2-3 days on analyzing this issue, I found the solution for this. I want to share this post by that in future no body will face this type of problem.
What is browser cache?
process of retrieving data. There are several types of caches, including web page caches, hardware caches, software caches and memory caches.
Caches are used to speed up a process so that data does not have to be
recomputed or fetched from its original location and, therefore, saves
time. The greater the requests that can be served from the cache, the
quicker system performance becomes.
of visited pages and then using that copy to render when you re-visit
that page. If the date on the page is the same date as the previously
stored copy, then the computer uses the one on your hard drive rather
than re-downloading it from the internet.
How to leverage browser cache?
Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0
Where:
Cache-Control
is for HTTP 1.1Pragma
is for HTTP 1.0Expires
is for proxies
1. Add .htaccess file on your web host/server.
.htaccess (Apache)
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg “access 1 year”
ExpiresByType image/jpeg “access 1 year”
ExpiresByType image/gif “access 1 year”
ExpiresByType image/png “access 1 year”
ExpiresByType text/css “access 1 month”
ExpiresByType text/html “access 1 month”
ExpiresByType application/pdf “access 1 month”
ExpiresByType text/x-javascript “access 1 month”
ExpiresByType application/x-shockwave-flash “access 1 month”
ExpiresByType image/x-icon “access 1 year”
ExpiresDefault “access 1 month”
</IfModule><FilesMatch “^.”>
Header set Pragma “no-cache”
Header set Cache-Control “no-cache”
</FilesMatch>## EXPIRES CACHING ##
Add this file to your web server in Root directory.
2. Disable browser caching by code.
Java Servlet
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0);
PHP
header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0');
ASP
Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" Response.addHeader "Pragma", "no-cache" Response.addHeader "Expires", "0"
ASP.NET
Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0");
Ruby on Rails
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' response.headers['Pragma'] = 'no-cache' response.headers['Expires'] = '0'
Python on Flask
resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" resp.headers["Pragma"] = "no-cache" resp.headers["Expires"] = "0"
Google Go
responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") responseWriter.Header().Set("Pragma", "no-cache") responseWriter.Header().Set("Expires", "0")
2. Use <meta> tags to turn off caching
<meta>
tags to the page(s) you wantto keep browsers from caching (the code must be in the
<head>
section of your page,for example right after
<title>
tag):<meta http-equiv="cache-control" content="no-cache" />
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="-1">
credit : cristian.sulea.net