Skip to content

Commit

Permalink
FEATURE: automatically update site to latest version of assets
Browse files Browse the repository at this point in the history
if a user neglects to move around the site it will prompt to do so 2 hours in
  • Loading branch information
SamSaffron committed Jan 15, 2014
1 parent abe8144 commit a247389
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
5 changes: 5 additions & 0 deletions app/assets/javascripts/discourse.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
}
},

requiresRefresh: function(){
var desired = Discourse.get("desiredAssetVersion");
return desired && Discourse.get("currentAssetVersion") !== desired;
}.property("currentAssetVersion", "desiredAssetVersion"),

assetVersion: function(prop, val) {
if(val) {
if(this.get("currentAssetVersion")){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ Discourse.addInitializer(function() {
Discourse.MessageBus.start();
Discourse.MessageBus.subscribe("/global/asset-version", function(version){
Discourse.set("assetVersion",version);

if(Discourse.get("requiresRefresh")) {
// since we can do this transparently for people browsing the forum
// hold back the message a couple of hours
setTimeout(function() {
bootbox.confirm(I18n.lookup("assets_changed_confirm"), function(){
document.location.reload();
});
}, 1000 * 60 * 120);
}

});
Discourse.KeyValueStore.init("discourse_", Discourse.MessageBus);
}, true);
10 changes: 3 additions & 7 deletions app/assets/javascripts/discourse/lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,9 @@ Discourse.URL = Em.Object.createWithMixins({
**/
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;
}
if(Discourse.get("requiresRefresh")){
document.location.href = path;
return;
}

var oldPath = window.location.pathname;
Expand Down
2 changes: 1 addition & 1 deletion app/views/common/_discourse_javascript.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
Discourse.SiteSettings = PreloadStore.get('siteSettings');
Discourse.Router.map(function() { Discourse.routeBuilder.call(this); });
Discourse.start()
Discourse.set('assetVersion','<%= Rails.application.assets.digest %>');
Discourse.set('assetVersion','<%= Discourse.assets_digest %>');
</script>

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

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
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ en:

loading: "Loading..."
close: "Close"
assets_changed_confirm: "Discourse has been updated, would you like to refresh to get the latest version?"
learn_more: "learn more..."

year: 'year'
Expand Down
14 changes: 14 additions & 0 deletions lib/discourse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def self.plugins
@plugins
end

def self.assets_digest
@assets_digest ||= begin
digest = Digest::MD5.hexdigest(ActionView::Base.assets_manifest.assets.values.sort.join)

channel = "/global/asset-version"
message = MessageBus.last_message(channel)

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

def self.authenticators
# TODO: perhaps we don't need auth providers and authenticators maybe one object is enough

Expand Down

2 comments on commit a247389

@riking
Copy link
Contributor

@riking riking commented on a247389 Jan 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I trigger this to test it from a developer instance from the server?

Triggering it from the client is easy: just Discourse.set('assetVersion', 'badvalue');

@SamSaffron
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To do it on dev I would do something like

rails c

MessageBus.publish '/global/asset-version' , 'xyz'

Please sign in to comment.