A platform for community discussion. Free, open, simple.
DEV: In development, refresh client when theme changes are made (#22978)
David Taylore76e0ad59241
diff --git a/app/assets/javascripts/discourse/app/instance-initializers/live-development.js b/app/assets/javascripts/discourse/app/instance-initializers/live-development.jsindex ba2c306ac05..132ad0782ce 100644--- a/app/assets/javascripts/discourse/app/instance-initializers/live-development.js+++ b/app/assets/javascripts/discourse/app/instance-initializers/live-development.js@@ -7,7 +7,7 @@ import { bind } from "discourse-common/utils/decorators";export default {initialize(owner) {this.messageBus = owner.lookup("service:message-bus");- const session = owner.lookup("service:session");+ this.session = owner.lookup("service:session");// Preserve preview_theme_id=## and pp=async-flamegraph parameters across pagesconst params = new URLSearchParams(window.location.search);@@ -40,7 +40,7 @@ export default {this.messageBus.subscribe("/file-change",this.onFileChange,- session.mbLastFileChangeId+ this.session.mbLastFileChangeId);},@@ -54,6 +54,14 @@ export default {if (me === "refresh") {// Refresh if necessarydocument.location.reload(true);+ } else if (me === "development-mode-theme-changed") {+ if (window.location.pathname.startsWith("/admin/customize/themes")) {+ // don't refresh users on routes which make theme changes - would be very inconvenient.+ // Instead, refresh on their next route navigation.+ this.session.requiresRefresh = true;+ } else {+ document.location.reload(true);+ }} else if (me.new_href && me.target) {let query = `link[data-target='${me.target}']`;diff --git a/app/models/theme.rb b/app/models/theme.rbindex 73225814553..de338ea363b 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -98,6 +98,9 @@ class Theme < ActiveRecord::Basechanged_colors.clearchanged_schemes.clear+ any_non_css_fields_changed =+ changed_fields.any? { |f| !(f.basic_scss_field? || f.extra_scss_field?) }+changed_fields.each(&:save!)changed_fields.clear@@ -126,6 +129,14 @@ class Theme < ActiveRecord::Baseself.theme_setting_requests_refresh = falseendend++ if any_non_css_fields_changed && should_refresh_development_clients?+ MessageBus.publish "/file-change", ["development-mode-theme-changed"]+ end+ end++ def should_refresh_development_clients?+ Rails.env.development?enddef update_child_components@@ -513,18 +524,20 @@ class Theme < ActiveRecord::Basechanged_fields << fieldendend- fieldelseif value.present? || upload_id.present?- theme_fields.build(- target_id: target_id,- value: value,- name: name,- type_id: type_id,- upload_id: upload_id,- )+ field =+ theme_fields.build(+ target_id: target_id,+ value: value,+ name: name,+ type_id: type_id,+ upload_id: upload_id,+ )+ changed_fields << fieldendend+ fieldenddef child_theme_ids=(theme_ids)diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rbindex 5f81715b4ac..8cae8b35391 100644--- a/spec/models/theme_spec.rb+++ b/spec/models/theme_spec.rb@@ -1093,4 +1093,30 @@ HTMLexpect(messages.count).to eq(0)endend++ describe "development experience" do+ it "sends 'development-mode-theme-changed event when non-css fields are updated" do+ Theme.any_instance.stubs(:should_refresh_development_clients?).returns(true)++ theme.set_field(target: :common, name: :scss, value: "body {background: green;}")++ messages =+ MessageBus+ .track_publish { theme.save! }+ .filter { |m| m.channel == "/file-change" }+ .map(&:data)++ expect(messages).not_to include("development-mode-theme-changed")++ theme.set_field(target: :common, name: :header, value: "<p>Hello world</p>")++ messages =+ MessageBus+ .track_publish { theme.save! }+ .filter { |m| m.channel == "/file-change" }+ .map(&:data)++ expect(messages).to include(["development-mode-theme-changed"])+ end+ endend