I have a WYSIWYG editor that I have built into the site and customised. There are a lot of Javascript files that only need to be loaded on the pages with the WYSIWYG editor; cur
You can put any Javascript files that you want to load manually in the public/javascripts/lib
directory of your application, and they will not be included in the assets pipeline. You can then load them manually on the pages that need them.
For instance, in one project, I use the Chosen jQuery plugin, and I load it like so:
Rails will source the public files from public/
, so you only need to reference your files from there (remove the public/
bit).
This project is fairly large, with 88 controllers, 662 actions, and a total of 38 custom javascript libraries that get used sporadically around the app, including markdown editors, charting libraries, and even jQuery UI.
To manage the sprawl and to keep each page as tight as possible, I have done 2 things: 1) in my controller, I set an instance variable,@page_libs
, to list the libs to load, and 2) the layout uses the values in @page_libs
to include the specialty Javascript when required.
A controller action might look like this:
def edit
@products = products.find(params[:id])
@page_libs = [:ui, :textile]
end
And the app/views/layouts/application.html.erb
includes this in the correct place:
<%- if @page_libs&.include?(:ui) || @page_libs&.include?(:table) %>
<% end -%>
<%- if @page_libs&.include?(:swiper) %>
<% end -%>
<%- if @page_libs&.include?(:table) %>
<% end -%>
<%- if @page_libs&.include?(:textile) %>
<% end -%>
Note that the first include is for jQuery UI, which I load from a CDN, rather than from my app's public
. This technique works just as well with external libraries, as well as those that you host. In fact, most pages in my app only depend on 2 external libraries (jQuery and Underscore.js), but have the option of loading up to 16 other Javascript libraries from external sources. Limiting external libraries on the page can significantly reduce your page load times, which is a direct performance boost for your application.
Sometimes, a Javascript library will include CSS components, as well. Or, you may even have page-specific CSS to include. The same approach can be taken with external stylesheet. These are the corresponding page-specific stylesheet "includes" for the above Javascript libraries:
<%- if @page_libs&.include?(:ui) %>
<% end -%>
<%- if @page_libs&.include?(:swiper) %>
<% end -%>
This way, I have a single point in the project to manage the libraries, regardless of how many (or few) are required for a page. I'll probably eventually create a series of custom before_action
handlers in the ApplicationController to define which the libraries a page needs included. Something like this:
before_action: :include_library_ui, only: [:new, :edit]
before_action: :include_library_swiper, only: [:show]
This would clean up the controller actions a bit more and make it easier to identify dependencies. However, given the size of my codebase and the pressing tasks remaining, I haven't taken this leap yet. Maybe it will inspire you to do so to start out.