用户不刷新时更新Web应用程序[关闭]

At my job, I write code for a web application, and as such new code will get pushed to the servers relatively frequently. We are running into a problem, though, where some of the users of this application tend to leave it open in a browser window for up to days at a time. They then run into this problem where:

  1. They enter data into the application which is still running on their machine from last week.
  2. They try to save their data, which occasionally won't work, since the code on the server is newer than the code on their local machine (i.e., the JavaScript on their client is sending data which differs from what the new code on the server expects).
  3. They can't simply refresh the page to re-download the JavaScript code that has changed, or they would lose data.

What is the correct way to deal with this situation? I have of course thought of the obvious solutions, like just telling these users not to do that, or just never changing my AJAX API so the requests won't fail, but I'm hoping that there's a better way of handling this.

In particular, our environment consists of PHP on the server, JavaScript on the client, and git used to manage source code. I am not particularly worried about supporting old versions of IE or etc. (at least regarding this issue).

In the past I've solved this by having a constantly polling script that polls for revision.txt or something similar on the server that has a version number in it that is incremented on each deploy.

When the version changes I either popup a message showing that they need to refresh / disable the UI or force refresh them (which is generally icky and could make them mad).

Something like this concept:

var createVersionChecker = function(seconds) {
  var version = null;

  var checkVersion = function() {
    $.get('/version.txt', function(response) {
      if(version == null) {
        version = response;
        return;
      }

      if(version != response) {
        // do some logic to show the user that they need to refresh
        // or force refresh them.. ick!
      }
    });
  };

  setInterval(checkVersion, seconds * 1000);
};

createVersionChecker(60);

I'd try to minimize breaking changes in the data passed from the client to the server. Now, I'm not saying that you can't make changes to the API; it's just that any changes that are made should generally be backwards-compatible with the previous version. In general, this means that if an API endpoint takes some parameters and you want to change them, if at all possible you should

  1. ignore parameters you don't care about (for when you remove a necessary parameter, but the older client is still ending it); and
  2. provide sensible defaults for data not provided (for when you add a parameter).

Similar things apply for data sent back from the server: the client should ignore things it doesn't recognize and apply sensible defaults when it doesn't get something expects. This is essentially the robustness principle: “Be conservative in what you send, be liberal in what you accept.”

Sure, it's not a slick solution, but when done well, it works decently and makes your software more robust.

While no replacement for proper robustness, you may also want to consider providing some version tag along with all your responses. If the user has just finished saving some data (so it's safe to refresh) and the received version differs from a version embedded in the client-side script, it would not be unreasonable to refresh the page. But this is no replacement for robustness.