From 73d207a56873625dedda8889b539f9c46663ef6b Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 14 Oct 2020 16:29:40 +0200 Subject: [PATCH] DEV: load styleguide assets only when needed (#10918) --- .../javascripts/discourse/app/lib/url.js | 1 + plugins/styleguide/plugin.rb | 6 +++++ .../spec/integration/assets_spec.rb | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 plugins/styleguide/spec/integration/assets_spec.rb diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js index c5ecda5d67e..335f33cd1c6 100644 --- a/app/assets/javascripts/discourse/app/lib/url.js +++ b/app/assets/javascripts/discourse/app/lib/url.js @@ -34,6 +34,7 @@ const SERVER_SIDE_ONLY = [ /^\/admin\/logs\/watched_words\/action\/[^\/]+\/download$/, /^\/pub\//, /^\/invites\//, + /^\/styleguide\//, ]; // The amount of height (in pixles) that we factor in when jumpEnd is called so diff --git a/plugins/styleguide/plugin.rb b/plugins/styleguide/plugin.rb index 47a5d7cfc8a..2e0703359cd 100644 --- a/plugins/styleguide/plugin.rb +++ b/plugins/styleguide/plugin.rb @@ -13,3 +13,9 @@ load File.expand_path('../lib/styleguide/engine.rb', __FILE__) Discourse::Application.routes.append do mount ::Styleguide::Engine, at: '/styleguide' end + +after_initialize do + register_asset_filter do |type, request| + request&.fullpath&.start_with?('/styleguide') + end +end diff --git a/plugins/styleguide/spec/integration/assets_spec.rb b/plugins/styleguide/spec/integration/assets_spec.rb new file mode 100644 index 00000000000..562730b7dd1 --- /dev/null +++ b/plugins/styleguide/spec/integration/assets_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Styleguide assets' do + before do + SiteSetting.styleguide_enabled = true + sign_in(Fabricate(:admin)) + end + + context 'visits homepage' do + it 'doesn’t load styleguide assets' do + get '/' + expect(response.body).to_not include('styleguide') + end + end + + context 'visits styleguide' do + it 'loads styleguide assets' do + get '/styleguide' + expect(response.body).to include('styleguide') + end + end +end