mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 22:08:01 +08:00
DEV: Refactor custom svg icon caching (#13483)
Previously, we were storing custom svg sprite paths in the cache. This is a problem because sprites in themes get stored as uploads, and the returned paths were files in the temporary download cache which could sometimes be cleaned up, resulting in a broken cache. I previously tried to fix this by skipping the missing files and clearing the cache, but that didn't work out well with CDNs. This PR stores the contents of the files in the custom_svg_sprites cache to avoid the problem of missing temp files. Also, plugin custom icons are only included if the plugin is enabled.
This commit is contained in:
@ -156,19 +156,6 @@ describe SvgSprite do
|
||||
expect(icons).to include("fly")
|
||||
end
|
||||
|
||||
it 'includes custom icons from a sprite in a theme' do
|
||||
theme = Fabricate(:theme)
|
||||
fname = "custom-theme-icon-sprite.svg"
|
||||
|
||||
upload = UploadCreator.new(file_from_fixtures(fname), fname, for_theme: true).create_for(-1)
|
||||
|
||||
theme.set_field(target: :common, name: SvgSprite.theme_sprite_variable_name, upload_id: upload.id, type: :theme_upload_var)
|
||||
theme.save!
|
||||
|
||||
expect(Upload.where(id: upload.id)).to be_exist
|
||||
expect(SvgSprite.bundle(theme.id)).to match(/my-custom-theme-icon/)
|
||||
end
|
||||
|
||||
context "s3" do
|
||||
let(:upload_s3) { Fabricate(:upload_s3) }
|
||||
|
||||
@ -191,8 +178,7 @@ describe SvgSprite do
|
||||
theme.save!
|
||||
|
||||
sprite_files = SvgSprite.custom_svg_sprites(theme.id).join("|")
|
||||
expect(sprite_files).to match(/#{upload_s3.sha1}/)
|
||||
expect(sprite_files).not_to match(/amazonaws/)
|
||||
expect(sprite_files).to match(/my-custom-theme-icon/)
|
||||
|
||||
SvgSprite.bundle(theme.id)
|
||||
expect(SvgSprite.cache.hash.keys).to include("custom_svg_sprites_#{theme.id}")
|
||||
@ -201,9 +187,8 @@ describe SvgSprite do
|
||||
File.delete external_copy.try(:path)
|
||||
|
||||
SvgSprite.bundle(theme.id)
|
||||
# when a file is missing, ensure that cache entry is cleared
|
||||
expect(SvgSprite.cache.hash.keys).to_not include("custom_svg_sprites_#{theme.id}")
|
||||
|
||||
# after a temp file is missing, bundling still works
|
||||
expect(SvgSprite.cache.hash.keys).to include("custom_svg_sprites_#{theme.id}")
|
||||
end
|
||||
end
|
||||
|
||||
@ -237,4 +222,62 @@ describe SvgSprite do
|
||||
group = Fabricate(:group, flair_icon: "far-building")
|
||||
expect(SvgSprite.bundle).to match(/far-building/)
|
||||
end
|
||||
|
||||
describe "#custom_svg_sprites" do
|
||||
it 'is empty by default' do
|
||||
expect(SvgSprite.custom_svg_sprites(nil)).to be_empty
|
||||
expect(SvgSprite.bundle).not_to be_empty
|
||||
end
|
||||
|
||||
context "with a plugin" do
|
||||
let :plugin1 do
|
||||
plugin1 = Plugin::Instance.new
|
||||
plugin1.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
|
||||
plugin1
|
||||
end
|
||||
|
||||
before do
|
||||
Discourse.plugins << plugin1
|
||||
plugin1.activate!
|
||||
end
|
||||
|
||||
after do
|
||||
Discourse.plugins.delete plugin1
|
||||
end
|
||||
|
||||
it "includes custom icons from plugins" do
|
||||
expect(SvgSprite.custom_svg_sprites(nil).size).to eq(1)
|
||||
expect(SvgSprite.bundle).to match(/custom-icon/)
|
||||
end
|
||||
end
|
||||
|
||||
it 'includes custom icons in a theme' do
|
||||
theme = Fabricate(:theme)
|
||||
fname = "custom-theme-icon-sprite.svg"
|
||||
|
||||
upload = UploadCreator.new(file_from_fixtures(fname), fname, for_theme: true).create_for(-1)
|
||||
|
||||
theme.set_field(target: :common, name: SvgSprite.theme_sprite_variable_name, upload_id: upload.id, type: :theme_upload_var)
|
||||
theme.save!
|
||||
|
||||
expect(Upload.where(id: upload.id)).to be_exist
|
||||
expect(SvgSprite.bundle(theme.id)).to match(/my-custom-theme-icon/)
|
||||
end
|
||||
|
||||
it 'includes custom icons in a child theme' do
|
||||
theme = Fabricate(:theme)
|
||||
fname = "custom-theme-icon-sprite.svg"
|
||||
child_theme = Fabricate(:theme, component: true)
|
||||
theme.add_relative_theme!(:child, child_theme)
|
||||
|
||||
upload = UploadCreator.new(file_from_fixtures(fname), fname, for_theme: true).create_for(-1)
|
||||
|
||||
child_theme.set_field(target: :common, name: SvgSprite.theme_sprite_variable_name, upload_id: upload.id, type: :theme_upload_var)
|
||||
child_theme.save!
|
||||
|
||||
expect(Upload.where(id: upload.id)).to be_exist
|
||||
expect(SvgSprite.bundle(theme.id)).to match(/my-custom-theme-icon/)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user