Skip to content

Commit

Permalink
Preload tinymce assets
Browse files Browse the repository at this point in the history
Tinymce loads the editor content css and its plugins on initialize.
Since we init tinymce only when its visible it takes long to init the
editor. Using browser preload features to make sure the assets
are already loaded.
  • Loading branch information
tvdeyen committed Nov 24, 2023
1 parent 635e0f2 commit bd50fac
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/views/alchemy/admin/tinymce/_setup.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<% asset_host = ActionController::Base.config.asset_host %>

<link rel="preload" href="<%= asset_host %><%= assets_prefix %>/tinymce/skins/alchemy/skin.min.css" as="style" />
<link rel="preload" href="<%= asset_host %><%= assets_prefix %>/tinymce/skins/alchemy/content.min.css" as="style" />
<% if Alchemy::Tinymce.init[:content_css] %>
<link rel="preload" href="<%= asset_host %><%= Alchemy::Tinymce.init[:content_css] %>" as="style" />
<% end %>
<% Alchemy::Tinymce.preloadable_plugins.each do |plugin| %>
<link rel="preload" href="<%= asset_host %><%= assets_prefix %>/tinymce/plugins/<%= plugin %>/plugin.min.js" as="script">
<% end %>

<script>
// Setting TinyMCE path.
var tinyMCEPreInit = {
Expand Down
8 changes: 7 additions & 1 deletion lib/alchemy/tinymce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Alchemy
module Tinymce
mattr_accessor :languages, :plugins

@@plugins = %w[alchemy_link anchor autoresize charmap code directionality fullscreen hr link lists paste tabfocus table]
DEFAULT_PLUGINS = %w[anchor autoresize charmap code directionality fullscreen hr link lists paste tabfocus table]

@@plugins = DEFAULT_PLUGINS + %w[alchemy_link]
@@init = {
skin: "alchemy",
width: "auto",
Expand Down Expand Up @@ -33,6 +35,10 @@ def init=(settings)
def init
@@init
end

def preloadable_plugins
@@plugins - DEFAULT_PLUGINS
end
end
end
end
52 changes: 52 additions & 0 deletions spec/features/admin/tinymce_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,56 @@
)
end
end

describe "assets preloading" do
it "should preload assets" do
visit admin_dashboard_path
expect(page)
.to have_css('link[rel="preload"][href="/assets/tinymce/skins/alchemy/skin.min.css"]')
.and have_css('link[rel="preload"][href="/assets/tinymce/skins/alchemy/content.min.css"]')
end

context "with asset host" do
around do |example|
host = ActionController::Base.config.asset_host
ActionController::Base.config.asset_host = "https://myhost.com"
example.run
ActionController::Base.config.asset_host = host
end

it "should preload assets from host" do
visit admin_dashboard_path
expect(page)
.to have_css('link[rel="preload"][href="https://myhost.com/assets/tinymce/skins/alchemy/skin.min.css"]')
.and have_css('link[rel="preload"][href="https://myhost.com/assets/tinymce/skins/alchemy/content.min.css"]')
end
end

context "when content_css is configured" do
before do
Alchemy::Tinymce.init = {content_css: "/assets/custom-stylesheet.css"}
end

it "should preload it" do
visit admin_dashboard_path
expect(page)
.to have_css('link[rel="preload"][href="/assets/custom-stylesheet.css"]')
end

context "with asset host" do
around do |example|
host = ActionController::Base.config.asset_host
ActionController::Base.config.asset_host = "https://myhost.com"
example.run
ActionController::Base.config.asset_host = host
end

it "should preload it from host" do
visit admin_dashboard_path
expect(page)
.to have_css('link[rel="preload"][href="https://myhost.com/assets/custom-stylesheet.css"]')
end
end
end
end
end
12 changes: 12 additions & 0 deletions spec/libraries/tinymce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,17 @@ module Alchemy
expect(Tinymce.init).to include(another_config)
end
end

describe ".preloadable_plugins" do
subject { Tinymce.preloadable_plugins }

before do
Tinymce.plugins += ["foo"]
end

it "returns all plugins without default plugins" do
is_expected.to eq(%w[alchemy_link foo])
end
end
end
end

0 comments on commit bd50fac

Please sign in to comment.