FIX: Preload readonly mode attribute seperately.

FIX: Preload readonly mode attribute seperately.

There are two problems I’m trying to tackle here.

  1. The site json is cached for anonymous users so readonly mode can be cached for up to 30 minutes which makes it confusing.

  2. We’ve already checked for readonly mode in the controller so having to check for readonly mode again in SiteSerializer is adding an extra Redis query on every request.

diff --git a/app/assets/javascripts/discourse/app/models/site.js b/app/assets/javascripts/discourse/app/models/site.js
index 8e6b8f2..0481485 100644
--- a/app/assets/javascripts/discourse/app/models/site.js
+++ b/app/assets/javascripts/discourse/app/models/site.js
@@ -132,7 +132,9 @@ Site.reopenClass(Singleton, {
   // The current singleton will retrieve its attributes from the `PreloadStore`.
   createCurrent() {
     const store = Discourse.__container__.lookup("service:store");
-    return store.createRecord("site", PreloadStore.get("site"));
+    const siteAttributes = PreloadStore.get("site");
+    siteAttributes["isReadOnly"] = PreloadStore.get("isReadOnly");
+    return store.createRecord("site", siteAttributes);
   },
 
   create() {
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index c59266e..eaaa515 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -539,6 +539,7 @@ class ApplicationController < ActionController::Base
     store_preloaded("customHTML", custom_html_json)
     store_preloaded("banner", banner_json)
     store_preloaded("customEmoji", custom_emoji)
+    store_preloaded("isReadOnly", @readonly_mode.to_s)
   end
 
   def preload_current_user_data
diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb
index d5bb901..96833f3 100644
--- a/app/serializers/site_serializer.rb
+++ b/app/serializers/site_serializer.rb
@@ -12,7 +12,6 @@ class SiteSerializer < ApplicationSerializer
     :top_menu_items,
     :anonymous_top_menu_items,
     :uncategorized_category_id, # this is hidden so putting it here
-    :is_readonly,
     :disabled_plugins,
     :user_field_max_length,
     :post_action_types,
@@ -95,10 +94,6 @@ class SiteSerializer < ApplicationSerializer
     SiteSetting.uncategorized_category_id
   end
 
-  def is_readonly
-    Discourse.readonly_mode?
-  end
-
   def disabled_plugins
     Discourse.disabled_plugin_names
   end

GitHub sha: 62ad4737

1 Like