dierre
dierre

Reputation: 7210

Ways to refresh a Backbone application after a software update

Right now I've embedded my Backbone application in an html page ( a classic index.html ). When I edit something and I go back to see the page, to see current changes I need to hit F5.

What would be the best practice in a case where the user is using an old version due to the page being cached by the browser?

I was thinking about using a jsp or a php page but I honestly was not able to find so much on Google.

Can you help me?

Upvotes: 1

Views: 102

Answers (1)

mor
mor

Reputation: 2313

There is a difference between reloading a page automagically when a file changes (watching a folder) and making sure that new users have the right version of your javascript application.

In the first case, I am guessing this is for development purposes and you will most likely want to use something like Live.js to achieve this.

In the second case, each time a returning user enters the application ( loads the index.html ), the server should issue a 304 - Not changed for the javascript file if the application has not changed and the file is cached in the browser or a 200 - OK with the file if it has changed and serve the new version. This has nothing to do with Backbone and the caching strategies are implemented on the server side.

If however you would like the application to periodically check for new versions while the user is using it and reload if there is a change ( I personnally think this is a bad idea, but it is possible ), here is a solution that would work.

You will need to have an entry point on the server side that only returns the application's version. This could be a semantic versioning, a hash, a name, etc... As long as it is different for each version.

main.js

var currentVersion;

// Assuming that the '/api/app-version' access point returns a string or number.
$.ajax({ url : '/api/app-version'})
 .done(function(data) { currentVersion = data });

// Poll the server every minute to watch for a new version.
// If the version is different, reload the application.
setInterval( function() {
  $.ajax({ url : '/api/app-version'})
   .done(function(data) { if ( data !== currentVersion ) window.location.reload() }); 
}, 60000);

Upvotes: 1

Related Questions