Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AJAX Browser Cache Problem

Posted on 2011-02-14
6
Medium Priority
?
508 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 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 61

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

772 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