Skip to content

Commit

Permalink
FEATURE: Automatically force a full refresh between pages if assets c…
Browse files Browse the repository at this point in the history
…hange
  • Loading branch information
SamSaffron committed Jan 15, 2014
1 parent 224a343 commit fd95dbe
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
13 changes: 12 additions & 1 deletion app/assets/javascripts/discourse.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,18 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
}
});
}
}
},

assetVersion: function(prop, val) {
if(val) {
if(this.get("currentAssetVersion")){
this.set("desiredAssetVersion", val);
} else {
this.set("currentAssetVersion", val);
}
}
return this.get("currentAssetVersion");
}.property()

});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
Discourse.addInitializer(function() {
Discourse.MessageBus.alwaysLongPoll = Discourse.Environment === "development";
Discourse.MessageBus.start();
Discourse.MessageBus.subscribe("/global/asset-version", function(version){
Discourse.set("assetVersion",version);
});
Discourse.KeyValueStore.init("discourse_", Discourse.MessageBus);
}, true);
10 changes: 10 additions & 0 deletions app/assets/javascripts/discourse/lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ Discourse.URL = Em.Object.createWithMixins({
@param {String} path The path we are routing to.
**/
routeTo: function(path) {

// If somehow our asset version changed, force a full reload of desired path
var desired = Discourse.get("desiredAssetVersion");
if(desired) {
if(Discourse.get("currentAssetVersion") !== desired){
document.location.href = path;
return;
}
}

var oldPath = window.location.pathname;
path = path.replace(/https?\:\/\/[^\/]+/, '');

Expand Down
6 changes: 4 additions & 2 deletions app/assets/javascripts/discourse/routes/application_route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Discourse.ApplicationRoute = Em.Route.extend({

actions: {

showLogin: function() {
Discourse.Route.showModal(this, 'login');
this.controllerFor('login').resetForm();
Expand Down Expand Up @@ -80,16 +81,17 @@ Discourse.ApplicationRoute = Em.Route.extend({
});
}

},
}
},

activate: function() {
this._super();
Em.run.next(function() {
Em.run.next(function() {
// Support for callbacks once the application has activated
Discourse.ApplicationRoute.trigger('activate');
});
}

});

RSVP.EventTarget.mixin(Discourse.ApplicationRoute);
1 change: 1 addition & 0 deletions app/views/common/_discourse_javascript.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Discourse.SiteSettings = PreloadStore.get('siteSettings');
Discourse.Router.map(function() { Discourse.routeBuilder.call(this); });
Discourse.start()
Discourse.set('assetVersion','<%= Rails.application.assets.digest %>');
</script>

<%= javascript_include_tag 'browser-update.js' %>
8 changes: 8 additions & 0 deletions config/initializers/04-message_bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@

MessageBus.cache_assets = !Rails.env.development?
MessageBus.enable_diagnostics

digest = Rails.application.assets.digest.to_s
channel = "/global/asset-version"
message = MessageBus.last_message(channel)

unless message && message.data == digest
MessageBus.publish channel, digest
end

0 comments on commit fd95dbe

Please sign in to comment.