Sunday, September 8, 2013

Dealing with Browser Cache


Did you try clearing the browser cache?, we often hear this when something doesn’t work in a web application. I believe most of developers know why we need to clear browser cache? or why do browsers need to do caching if it’s not such a good thing to do?

Browsers have in-built mechanism of caching webpages including all its supporting files (e.g. javascript, images or css style sheets) to avoid downloading the same content again and again. This greatly enhances the page loading speed, reduced network traffic and server load. This will work just fine until you modify a file (e.g. a javascript) and upload it on server and browser still picking up old file from its cache. In this case functionality related to this new change may not work. It can be easily fixed by clearing the browser cache but is not an ideal solution especially if you have a large audiences. 

You can also tell browsers not to cache a particular file or reduce the period they keep it cached. There are guidelines as to what type of files should be cached and what shouldn’t be cached (e.g. pages that interact with database and are updated regularly should never be cached). Disabling caching completely may degrade the overall page loading experience.

What is the idea way to handle it?
Whenever you modify a js or css file, you can tell browse that it’s a new file by putting a fake parameter in file name. e.g.

src=”scripts/jquery/jquery-1.7.1_min.js”
src=”scripts/jquery/jquery-1.7.1_min.js?version=1.1”

In each subsequent changes you need to just change the parameter value to something different. E.g.
src=”scripts/jquery/jquery-1.7.1_min.js?version=1.2”

You may find it tedious to change all references every time you modify a js file. To overcome this make the parameter value dynamic by using resource file and bean message (you can look for equivalent of 'bean message' if you are using language other than jsp).

<script type="text/javascript" src="scripts/jquery/jquery-1.7.1_min.js?version=<bean:message bundle="mybundle" key="js.jquery.version"/>"></script>

So every time you modify a javascript file, make sure you update its version/fake param value so that you never have to ask users to clear the browser cache! :-)


Happy coding!

Monday, April 1, 2013

Google Nose

Well! here is another beginning of a year or I would rather call new financial year - April 2013. Today, Google launched a new product called Google Nose. It is a search based service which allows you to smell the stuffs around the world! As we all know Google has been the most innovative company around the world and bringing something like ‘Google Nose’ isn’t a surprising news. We all know that Google is always churning the wheel to get the new things out!

The moment I saw this new search service on the Google (www.google.com) I couldn’t control myself from having a look at it. Before I could actually use this service I looked at the How it works section (http://www.google.com/intl/en/landing/nose/) . It had a quick video which shows how helpful this new ‘Google Nose’ is. Now it was the time for me to try out this new service.


I went to Google Nose search page and quickly keyed in ‘Rose’. A quick search result appeared and in right side of page which has a button ‘Smell’. I clicked on the smell button then brought my nose close to the monitor and pressed the enter button (as described in the help section) but I couldn’t smell anything. I tried it few more times but nothing happened! Immediately after this I figured out that, it is nothing but the April fool trick from Google!



Happy April Fool's Day! :-)