Link to home
Start Free TrialLog in
Avatar of numberkruncher
numberkruncherFlag for United Kingdom of Great Britain and Northern Ireland

asked on

AJAX Browser Cache Problem

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!
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

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

using jquery you can specify an option to mention no-cache to be used
http://api.jquery.com/jQuery.ajax/
Avatar of numberkruncher

ASKER

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,
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);



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).