Solved

AJAX Browser Cache Problem

Posted on 2011-02-14
6
501 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

830 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