FIX: Allow `file-change` events soon after reloading (#13065)

FIX: Allow file-change events soon after reloading (#13065)

This patch remembers the last id for the file-change event and uses it to initialize the client side watcher. This should help fix the issue where styles are not reloaded client side if the browser refreshed.

diff --git a/app/assets/javascripts/discourse/app/initializers/live-development.js b/app/assets/javascripts/discourse/app/initializers/live-development.js
index 2c15c2f..e657240 100644
--- a/app/assets/javascripts/discourse/app/initializers/live-development.js
+++ b/app/assets/javascripts/discourse/app/initializers/live-development.js
@@ -8,6 +8,7 @@ export default {
   name: "live-development",
   initialize(container) {
     const messageBus = container.lookup("message-bus:main");
+    const session = container.lookup("session:main");
 
     // Preserve preview_theme_id=## and pp=async-flamegraph parameters across pages
     const params = new URLSearchParams(window.location.search);
@@ -48,32 +49,36 @@ export default {
     }
 
     // Observe file changes
-    messageBus.subscribe("/file-change", function (data) {
-      if (Handlebars.compile && !Ember.TEMPLATES.empty) {
-        // hbs notifications only happen in dev
-        Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
-      }
-      data.forEach((me) => {
-        if (me === "refresh") {
-          // Refresh if necessary
-          document.location.reload(true);
-        } else {
-          $("link").each(function () {
-            if (me.hasOwnProperty("theme_id") && me.new_href) {
-              const target = $(this).data("target");
-              const themeId = $(this).data("theme-id");
-              if (
-                target === me.target &&
-                (!themeId || themeId === me.theme_id)
-              ) {
-                refreshCSS(this, null, me.new_href);
-              }
-            } else if (this.href.match(me.name) && (me.hash || me.new_href)) {
-              refreshCSS(this, me.hash, me.new_href);
-            }
-          });
+    messageBus.subscribe(
+      "/file-change",
+      function (data) {
+        if (Handlebars.compile && !Ember.TEMPLATES.empty) {
+          // hbs notifications only happen in dev
+          Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
         }
-      });
-    });
+        data.forEach((me) => {
+          if (me === "refresh") {
+            // Refresh if necessary
+            document.location.reload(true);
+          } else {
+            $("link").each(function () {
+              if (me.hasOwnProperty("theme_id") && me.new_href) {
+                const target = $(this).data("target");
+                const themeId = $(this).data("theme-id");
+                if (
+                  target === me.target &&
+                  (!themeId || themeId === me.theme_id)
+                ) {
+                  refreshCSS(this, null, me.new_href);
+                }
+              } else if (this.href.match(me.name) && (me.hash || me.new_href)) {
+                refreshCSS(this, me.hash, me.new_href);
+              }
+            });
+          }
+        });
+      },
+      session.mbLastFileChangeId
+    );
   },
 };
diff --git a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js
index ceb7796..95e181f 100644
--- a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js
+++ b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js
@@ -83,6 +83,10 @@ export default {
     session.disableCustomCSS = setupData.disableCustomCss === "true";
     session.markdownItURL = setupData.markdownItUrl;
 
+    if (setupData.mbLastFileChangeId) {
+      session.mbLastFileChangeId = parseInt(setupData.mbLastFileChangeId, 10);
+    }
+
     if (setupData.safeMode) {
       session.safe_mode = setupData.safeMode;
     }
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 624d332..1709230 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -539,6 +539,7 @@ module ApplicationHelper
       if ENV['DEBUG_PRELOADED_APP_DATA']
         setup_data[:debug_preloaded_app_data] = true
       end
+      setup_data[:mb_last_file_change_id] = MessageBus.last_id('/file-change')
     end
 
     if guardian.can_enable_safe_mode? && params["safe_mode"]

GitHub sha: 32d6d830

This commit appears in #13065 which was approved by davidtaylorhq. It was merged by eviltrout.