From fd95dbe75a0f09f07a3c3f0f51e8e9762554f474 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 14 Jan 2014 16:59:08 +1100 Subject: [PATCH] FEATURE: Automatically force a full refresh between pages if assets change --- app/assets/javascripts/discourse.js | 13 ++++++++++++- .../discourse/initializers/1_init_message_bus.js | 3 +++ app/assets/javascripts/discourse/lib/url.js | 10 ++++++++++ .../discourse/routes/application_route.js | 6 ++++-- app/views/common/_discourse_javascript.html.erb | 1 + config/initializers/04-message_bus.rb | 8 ++++++++ 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index 97f4f1a31..8ffa2e47a 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -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() }); diff --git a/app/assets/javascripts/discourse/initializers/1_init_message_bus.js b/app/assets/javascripts/discourse/initializers/1_init_message_bus.js index d4397f535..fafac2aec 100644 --- a/app/assets/javascripts/discourse/initializers/1_init_message_bus.js +++ b/app/assets/javascripts/discourse/initializers/1_init_message_bus.js @@ -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); diff --git a/app/assets/javascripts/discourse/lib/url.js b/app/assets/javascripts/discourse/lib/url.js index bf5b8f1b6..8d2789629 100644 --- a/app/assets/javascripts/discourse/lib/url.js +++ b/app/assets/javascripts/discourse/lib/url.js @@ -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?\:\/\/[^\/]+/, ''); diff --git a/app/assets/javascripts/discourse/routes/application_route.js b/app/assets/javascripts/discourse/routes/application_route.js index be96c9ca8..babec6215 100644 --- a/app/assets/javascripts/discourse/routes/application_route.js +++ b/app/assets/javascripts/discourse/routes/application_route.js @@ -9,6 +9,7 @@ Discourse.ApplicationRoute = Em.Route.extend({ actions: { + showLogin: function() { Discourse.Route.showModal(this, 'login'); this.controllerFor('login').resetForm(); @@ -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); diff --git a/app/views/common/_discourse_javascript.html.erb b/app/views/common/_discourse_javascript.html.erb index ab3f91e42..cf3da5bec 100644 --- a/app/views/common/_discourse_javascript.html.erb +++ b/app/views/common/_discourse_javascript.html.erb @@ -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 %>'); <%= javascript_include_tag 'browser-update.js' %> diff --git a/config/initializers/04-message_bus.rb b/config/initializers/04-message_bus.rb index efc333f2d..2768d625f 100644 --- a/config/initializers/04-message_bus.rb +++ b/config/initializers/04-message_bus.rb @@ -37,3 +37,11 @@ end 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