Solved

AJAX Browser Cache Problem

Posted on 2011-02-14
6
502 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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 53

Expert Comment

by:Huseyin KAHRAMAN
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

734 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