Skip to content

Commit

Permalink
FEATURE: Calculate sprite-sheet based on currently active themes (#6973)
Browse files Browse the repository at this point in the history
Previously there was only one sprite sheet, which always included icons from all themes even if they were disabled
  • Loading branch information
davidtaylorhq committed Feb 6, 2019
1 parent ba9cc83 commit f3cfce4
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 51 deletions.
7 changes: 4 additions & 3 deletions app/controllers/svg_sprite_controller.rb
Expand Up @@ -10,12 +10,13 @@ def show
no_cookies

RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
theme_ids = params[:theme_ids].split(",").map(&:to_i)

if SvgSprite.version != params[:version]
return redirect_to path(SvgSprite.path)
if SvgSprite.version(theme_ids) != params[:version]
return redirect_to path(SvgSprite.path(theme_ids))
end

svg_sprite = "window.__svg_sprite = #{SvgSprite.bundle.inspect};"
svg_sprite = "window.__svg_sprite = #{SvgSprite.bundle(theme_ids).inspect};"

response.headers["Last-Modified"] = 10.years.ago.httpdate
response.headers["Content-Length"] = svg_sprite.bytesize.to_s
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/application_helper.rb
Expand Up @@ -382,7 +382,7 @@ def topic_featured_link_domain(link)

def theme_ids
if customization_disabled?
nil
[nil]
else
request.env[:resolved_theme_ids]
end
Expand Down Expand Up @@ -454,11 +454,11 @@ def client_side_setup_data
asset_version: Discourse.assets_digest,
disable_custom_css: loading_admin?,
highlight_js_path: HighlightJs.path,
svg_sprite_path: SvgSprite.path,
svg_sprite_path: SvgSprite.path(theme_ids),
}

if Rails.env.development?
setup_data[:svg_icon_list] = SvgSprite.all_icons
setup_data[:svg_icon_list] = SvgSprite.all_icons(theme_ids)
end

if guardian.can_enable_safe_mode? && params["safe_mode"]
Expand Down
1 change: 1 addition & 0 deletions app/models/theme.rb
Expand Up @@ -121,6 +121,7 @@ def self.clear_default!
end

def self.transform_ids(ids, extend: true)
return [] if ids.nil?
get_set_cache "#{extend ? "extended_" : ""}transformed_ids_#{ids.join("_")}" do
next [] if ids.blank?

Expand Down
4 changes: 4 additions & 0 deletions app/models/theme_field.rb
Expand Up @@ -7,6 +7,10 @@ class ThemeField < ActiveRecord::Base
belongs_to :upload
has_one :javascript_cache, dependent: :destroy

after_commit do |field|
SvgSprite.expire_cache if field.target_id == Theme.targets[:settings]
end

scope :find_by_theme_ids, ->(theme_ids) {
return none unless theme_ids.present?

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -453,7 +453,7 @@
# in most production settings this is bypassed
get "letter_avatar_proxy/:version/letter/:letter/:color/:size.png" => "user_avatars#show_proxy_letter"

get "svg-sprite/:hostname/svg-:version.js" => "svg_sprite#show", format: false, constraints: { hostname: /[\w\.-]+/, version: /\h{40}/ }
get "svg-sprite/:hostname/svg-:theme_ids-:version.js" => "svg_sprite#show", format: false, constraints: { hostname: /[\w\.-]+/, version: /\h{40}/, theme_ids: /([0-9]+(,[0-9]+)*)?/ }
get "svg-sprite/search/:keyword" => "svg_sprite#search", format: false, constraints: { keyword: /[-a-z0-9\s\%]+/ }

get "highlight-js/:hostname/:version.js" => "highlight_js#show", format: false, constraints: { hostname: /[\w\.-]+/ }
Expand Down
65 changes: 33 additions & 32 deletions lib/svg_sprite/svg_sprite.rb
Expand Up @@ -188,39 +188,37 @@ module SvgSprite
SVG_SPRITE_PATHS = Dir.glob(["#{Rails.root}/vendor/assets/svg-icons/**/*.svg",
"#{Rails.root}/plugins/*/svg-icons/*.svg"])

def self.svg_sprite_cache
@svg_sprite_cache ||= DistributedCache.new('svg_sprite')
def self.all_icons(theme_ids = [])
get_set_cache("icons_#{Theme.transform_ids(theme_ids).join(',')}") do
Set.new()
.merge(settings_icons)
.merge(plugin_icons)
.merge(badge_icons)
.merge(group_icons)
.merge(theme_icons(theme_ids))
.delete_if { |i| i.blank? || i.include?("/") }
.map! { |i| process(i.dup) }
.merge(SVG_ICONS)
.sort
end
end

def self.all_icons
Set.new()
.merge(settings_icons)
.merge(plugin_icons)
.merge(badge_icons)
.merge(group_icons)
.merge(theme_icons)
.delete_if { |i| i.blank? || i.include?("/") }
.map! { |i| process(i.dup) }
.merge(SVG_ICONS)
.sort
def self.version(theme_ids = [])
get_set_cache("version_#{Theme.transform_ids(theme_ids).join(',')}") do
Digest::SHA1.hexdigest(all_icons(theme_ids).join('|'))
end
end

def self.rebuild_cache
icons = all_icons
svg_sprite_cache['icons'] = icons
svg_sprite_cache['version'] = Digest::SHA1.hexdigest(icons.join('|'))
def self.path(theme_ids = [])
"/svg-sprite/#{Discourse.current_hostname}/svg-#{theme_ids&.join(",")}-#{version(theme_ids)}.js"
end

def self.expire_cache
svg_sprite_cache.clear
end

def self.version
svg_sprite_cache['version'] || rebuild_cache
cache&.clear
end

def self.bundle
icons = svg_sprite_cache['icons'] || all_icons
def self.bundle(theme_ids = [])
icons = all_icons(theme_ids)

doc = File.open("#{Rails.root}/vendor/assets/svg-icons/fontawesome/solid.svg") { |f| Nokogiri::XML(f) }
fa_license = doc.at('//comment()').text
Expand All @@ -240,7 +238,6 @@ def self.bundle

svg_file.css('symbol').each do |sym|
icon_id = prepare_symbol(sym, svg_filename)

if icons.include? icon_id
sym.attributes['id'].value = icon_id
sym.css('title').each(&:remove)
Expand Down Expand Up @@ -286,10 +283,6 @@ def self.prepare_symbol(symbol, svg_filename)
icon_id
end

def self.path
"/svg-sprite/#{Discourse.current_hostname}/svg-#{version}.js"
end

def self.settings_icons
# includes svg_icon_subset and any settings containing _icon (incl. plugin settings)
site_setting_icons = []
Expand Down Expand Up @@ -319,11 +312,11 @@ def self.group_icons
Group.where("flair_url LIKE '%fa-%'").pluck(:flair_url).uniq
end

def self.theme_icons
def self.theme_icons(theme_ids)
theme_icon_settings = []

# Theme.all includes default values
Theme.all.each do |theme|
# Need to load full records for default values
Theme.where(id: Theme.transform_ids(theme_ids)).each do |theme|
settings = theme.cached_settings.each do |key, value|
if key.to_s.include?("_icon") && String === value
theme_icon_settings |= value.split('|')
Expand All @@ -347,4 +340,12 @@ def self.process(icon_name)
FA_ICON_MAP.each { |k, v| icon_name.sub!(k, v) }
fa4_to_fa5_names[icon_name] || icon_name
end

def self.get_set_cache(key)
cache[key] ||= yield
end

def self.cache
@cache ||= DistributedCache.new('svg_sprite')
end
end
46 changes: 37 additions & 9 deletions spec/components/svg_sprite/svg_sprite_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe SvgSprite do

before do
SvgSprite.rebuild_cache
SvgSprite.expire_cache
end

it 'can generate a bundle' do
Expand All @@ -12,6 +12,15 @@
expect(bundle).to match(/angle-double-down/)
end

it 'can generate paths' do
version = SvgSprite.version # Icons won't change for this test
expect(SvgSprite.path).to eq("/svg-sprite/#{Discourse.current_hostname}/svg--#{version}.js")
expect(SvgSprite.path([1, 2])).to eq("/svg-sprite/#{Discourse.current_hostname}/svg-1,2-#{version}.js")

# Safe mode
expect(SvgSprite.path([nil])).to eq("/svg-sprite/#{Discourse.current_hostname}/svg--#{version}.js")
end

it 'can search for a specific FA icon' do
expect(SvgSprite.search("fa-heart")).to match(/heart/)
expect(SvgSprite.search("poo-storm")).to match(/poo-storm/)
Expand Down Expand Up @@ -51,26 +60,43 @@

it 'includes icons defined in theme settings' do
theme = Fabricate(:theme)
theme.set_field(target: :settings, name: :yaml, value: "custom_icon: magic")

# Works for default settings:
theme.set_field(target: :settings, name: :yaml, value: "custom_icon: dragon")
theme.save!
expect(SvgSprite.all_icons([theme.id])).to include("dragon")

# TODO: add test for default settings values
# Automatically purges cache when default changes:
theme.set_field(target: :settings, name: :yaml, value: "custom_icon: gamepad")
theme.save!
expect(SvgSprite.all_icons([theme.id])).to include("gamepad")

# Works when applying override
theme.update_setting(:custom_icon, "gas-pump")
expect(SvgSprite.all_icons).to include("gas-pump")
expect(SvgSprite.all_icons([theme.id])).to include("gas-pump")

# Works when changing override
theme.update_setting(:custom_icon, "gamepad")
expect(SvgSprite.all_icons).to include("gamepad")
expect(SvgSprite.all_icons).not_to include("gas-pump")
expect(SvgSprite.all_icons([theme.id])).to include("gamepad")
expect(SvgSprite.all_icons([theme.id])).not_to include("gas-pump")

# FA5 syntax
theme.update_setting(:custom_icon, "fab fa-bandcamp")
expect(SvgSprite.all_icons).to include("fab-bandcamp")
expect(SvgSprite.all_icons([theme.id])).to include("fab-bandcamp")

# Internal Discourse syntax + multiple icons
theme.update_setting(:custom_icon, "fab-android|dragon")
expect(SvgSprite.all_icons).to include("fab-android")
expect(SvgSprite.all_icons).to include("dragon")
expect(SvgSprite.all_icons([theme.id])).to include("fab-android")
expect(SvgSprite.all_icons([theme.id])).to include("dragon")

# Check themes don't leak into non-theme sprite sheet
expect(SvgSprite.all_icons).not_to include("dragon")

# Check components are included
theme.update(component: true)
parent_theme = Fabricate(:theme)
parent_theme.add_child_theme!(theme)
expect(SvgSprite.all_icons([parent_theme.id])).to include("dragon")
end

it 'includes icons from SiteSettings' do
Expand All @@ -82,10 +108,12 @@
expect(all_icons).to include("fab-bandcamp")

SiteSetting.svg_icon_subset = nil
SvgSprite.expire_cache
expect(SvgSprite.all_icons).not_to include("drafting-compass")

# does not fail on non-string setting
SiteSetting.svg_icon_subset = false
SvgSprite.expire_cache
expect(SvgSprite.all_icons).to be_truthy
end

Expand Down
12 changes: 9 additions & 3 deletions spec/requests/svg_sprite_controller_spec.rb
Expand Up @@ -4,17 +4,23 @@

context 'show' do
before do
SvgSprite.rebuild_cache
SvgSprite.expire_cache
end

it "should return bundle when version is current" do
get "/svg-sprite/#{Discourse.current_hostname}/svg-#{SvgSprite.version}.js"
get "/svg-sprite/#{Discourse.current_hostname}/svg--#{SvgSprite.version}.js"
expect(response.status).to eq(200)

theme = Fabricate(:theme)
theme.set_field(target: :settings, name: :yaml, value: "custom_icon: dragon")
theme.save!
get "/svg-sprite/#{Discourse.current_hostname}/svg-#{theme.id}-#{SvgSprite.version([theme.id])}.js"
expect(response.status).to eq(200)
end

it "should redirect to current version" do
random_hash = Digest::SHA1.hexdigest("somerandomstring")
get "/svg-sprite/#{Discourse.current_hostname}/svg-#{random_hash}.js"
get "/svg-sprite/#{Discourse.current_hostname}/svg--#{random_hash}.js"

expect(response.status).to eq(302)
expect(response.location).to include(SvgSprite.version)
Expand Down

0 comments on commit f3cfce4

Please sign in to comment.