PERF: remove superflous queries from initial page loads
stop doing expensive work to figure out discourse style sheet
This commit is contained in:
parent
23ad68678e
commit
b2af49251d
|
@ -38,6 +38,8 @@ class Category < ActiveRecord::Base
|
||||||
after_destroy :publish_categories_list
|
after_destroy :publish_categories_list
|
||||||
after_update :rename_category_definition, if: :name_changed?
|
after_update :rename_category_definition, if: :name_changed?
|
||||||
|
|
||||||
|
after_save :publish_discourse_stylesheet
|
||||||
|
|
||||||
has_one :category_search_data
|
has_one :category_search_data
|
||||||
belongs_to :parent_category, class_name: 'Category'
|
belongs_to :parent_category, class_name: 'Category'
|
||||||
has_many :subcategories, class_name: 'Category', foreign_key: 'parent_category_id'
|
has_many :subcategories, class_name: 'Category', foreign_key: 'parent_category_id'
|
||||||
|
@ -362,6 +364,10 @@ SQL
|
||||||
topic.update_column(:title, I18n.t("category.topic_prefix", category: name))
|
topic.update_column(:title, I18n.t("category.topic_prefix", category: name))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def publish_discourse_stylesheet
|
||||||
|
MessageBus.publish("/discourse_stylesheet", self.name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -9,6 +9,7 @@ class ColorScheme < ActiveRecord::Base
|
||||||
scope :current_version, ->{ where(versioned_id: nil) }
|
scope :current_version, ->{ where(versioned_id: nil) }
|
||||||
|
|
||||||
after_destroy :destroy_versions
|
after_destroy :destroy_versions
|
||||||
|
after_save :publish_discourse_stylesheet
|
||||||
|
|
||||||
validates_associated :color_scheme_colors
|
validates_associated :color_scheme_colors
|
||||||
|
|
||||||
|
@ -93,6 +94,10 @@ class ColorScheme < ActiveRecord::Base
|
||||||
ColorScheme.where(versioned_id: self.id).destroy_all
|
ColorScheme.where(versioned_id: self.id).destroy_all
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def publish_discourse_stylesheet
|
||||||
|
MessageBus.publish("/discourse_stylesheet", self.name)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -7,13 +7,37 @@ class DiscourseStylesheets
|
||||||
MANIFEST_FULL_PATH = "#{MANIFEST_DIR}/stylesheet-manifest"
|
MANIFEST_FULL_PATH = "#{MANIFEST_DIR}/stylesheet-manifest"
|
||||||
|
|
||||||
@lock = Mutex.new
|
@lock = Mutex.new
|
||||||
|
@links = {}
|
||||||
|
|
||||||
|
def self.ensure_subscribed!
|
||||||
|
unless @subscribed
|
||||||
|
@lock.synchronize do
|
||||||
|
MessageBus.subscribe("/discourse_stylesheet") do |message|
|
||||||
|
@lock.synchronize do
|
||||||
|
@links[message.site_id] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@subscribed = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.stylesheet_link_tag(target = :desktop)
|
def self.stylesheet_link_tag(target = :desktop)
|
||||||
builder = self.new(target)
|
ensure_subscribed!
|
||||||
@lock.synchronize do
|
@lock.synchronize do
|
||||||
builder.compile unless File.exists?(builder.stylesheet_fullpath)
|
links = (@links[RailsMultisite::ConnectionManagement.current_db] ||= {})
|
||||||
builder.ensure_digestless_file
|
tag = links[target]
|
||||||
%[<link href="#{Rails.env.production? ? builder.stylesheet_relpath : builder.stylesheet_relpath_no_digest + '?body=1'}" media="all" rel="stylesheet" />].html_safe
|
|
||||||
|
if !tag
|
||||||
|
builder = self.new(target)
|
||||||
|
builder.compile unless File.exists?(builder.stylesheet_fullpath)
|
||||||
|
builder.ensure_digestless_file
|
||||||
|
tag = %[<link href="#{Rails.env.production? ? builder.stylesheet_relpath : builder.stylesheet_relpath_no_digest + '?body=1'}" media="all" rel="stylesheet" />].html_safe
|
||||||
|
|
||||||
|
links[target] = tag
|
||||||
|
end
|
||||||
|
|
||||||
|
tag
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue