kales33
kales33

Reputation: 690

Ruby Rails precompiling assets vs. using the asset pipeline

I'm wondering when to use Rails.application.config.assets.precompile to include Javascript plugins, and when to use the asset pipeline. From what I understand, the asset pipeline minifies all files listed in it, and it makes this minified file available to the entire application. However, you can use Rails.application.config.assets.precompile in config/initializers/assets.rb to allow a plugin to be used only on specific pages. Is this understanding correct?

For instance, if I have the lines below in my config/initializers/assets.rb file:

Rails.application.config.assets.precompile += %w( plugins/footable/angular-footable.js )
Rails.application.config.assets.precompile += %w( plugins/footable/footable.all.min.js )
Rails.application.config.assets.precompile += %w( plugins/footable/footable.core.css )

Then I can use the Foo Table plugin in my app/views/users/index.html.erb file:

<%= javascript_include_tag 'plugins/footable/angular-footable' %>
<%= javascript_include_tag 'plugins/footable/footable.all.min' %>
<%= stylesheet_link_tag 'plugins/footable/footable.core' %>

Is this a proper usage?

The reason I ask is because I want to use the Foo Table plugin (among others) only on the app/views/users/index.html.erb page. I thought about including the plugin in the asset pipeline, but I didn't want to bog down the rest of my application with Javascript code that was only needed by one page.

Upvotes: 1

Views: 238

Answers (1)

kales33
kales33

Reputation: 690

I found an answer to my own question on this website: https://launchschool.com/blog/rails-asset-pipeline-best-practices. Basically, the answer, is yes, my understanding is correct.

Here are the relevant pieces of the article: enter image description here

enter image description here

Upvotes: 4

Related Questions