Solved

AJAX Browser Cache Problem

Posted on 2011-02-14
6
492 Views
Last Modified: 2012-05-11
I am working on a web application which stays on the same page for a long time.

The application loads external JavaScript (by AJAX) as they are needed.

Somtimes (but not very often) scripts will be changed and the web browser will be using old cache. To improve this I have added a version number which is updated whenever any changes are made to the system. But this is not a conclusive solution because the new version number is not known until the next page change (which could be some time!!)

The only solution that I can concieve is:
function get_latest_version() {
    var version = null;

    jQuery.ajax({
        url: base_url + 'check-version.php',
        async: true,
        success: function(data) { version = data; }
    });

    // If version was not resolved, just assume new version.
    return version || new Date().getTime();
}


function load_script(script) {
    // Send request to server, ask what its version is at this point in time!
    var version = get_latest_version();

    var url = base_url + 'js/' + script + '?v=' + version;

    // Use jQuery to dynamically load JavaScript.
}

Open in new window

Is it worth asking the server what version it is (a very simple procedure with single integer response)? Or should I just have it download the script every single time?


I would appreciate your expert advice!
0
Comment
Question by:numberkruncher
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34889480
no need to ask new version data from server before downloading a script, just appending the date would do.

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34889487
using jquery you can specify an option to mention no-cache to be used
http://api.jquery.com/jQuery.ajax/
0
 
LVL 13

Author Comment

by:numberkruncher
ID: 34889545
Thanks, I had meant to include cache: false in the 'get_latest_version' function in my question...

True, but that means that it has to download the entire script every single time that it is needed (when it could be cached for future re-use). A simple version number request will usually eliminate the need to download the script.

Is it worth bugging the server twice, or should it just be downloading the script every time (even though the script probably hasn't changed)?

Sorry if it sounds like I am asking the same question again, I am just trying to confirm that specific point. Thanks again,
0
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!

 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 34889598
<<Is it worth bugging the server twice, or should it just be downloading the script every time (even though the script probably hasn't changed)?>>
it actually begs a question that why are you downloading it again? can't you just fetch it once and set a flag against it?
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34889674
no nehow big is your script? a few MB? if it is short like a few hunder K, then make it simple

version = 'V' + (new Date()).getTime();
var url = base_url + 'js/' + script + '?v=' + version;

and load every time... use a substr of (or modulus function) version to refresh it every hour/x min/x hours etc...

var url = base_url + 'js/' + script + '?v=' + version.substring(0,10);



0
 
LVL 13

Author Closing Comment

by:numberkruncher
ID: 34896342
Thank you for making me rethink it from that angle.

The chances are that (outdated and possibly broken) scripts are already flowing through the veins of the web browser VM.

I think that I need to learn more about ways to deal with consumers when the website is going to be updated.

Perhaps it would just be a case of warning them a day or two in advance and then terminating all user sessions after the changes have been made to force page reloads (which will then load fresh scripts).
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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

19 Experts available now in Live!

Get 1:1 Help Now