Reputation: 1446
I was in chapter 5 of the Ruby on Rails, and I come across "Asset pipeline" when R-o-R detect and jam all css and js files into one.
It's true that Browser will have to make less requests, but at the same time, the browser will potentially have to process more code than it should?
For example, I use a slider jquery plugin in gallery page, since R-o-R fuse the plugin with all other js files into 1 javascript.js, the plugin will be included in about page, home page and so on...
Can I import separated files for separated pages? and how?
Thanks
Upvotes: 1
Views: 867
Reputation: 6419
One approach for doing page-specific files is to tell the asset pipeline to generate extra files, and then manually load them. For the first part, telling ROR to generate a particular file, you'd adjust your config/application.rb
file and add:
config.assets.precompile += %w(my_special_file1.js my_special_file2.js)
Add any files you want created to that list. Treat each of those files as you would the application.js
file.
Then, in your page, you'd simply load the desired file using javascript_include_tag
.
While the example given about a particular jQuery plugin probably wouldn't see much benefit from this approach, as a general strategy, it's useful if you wanted to include a different set of code on different versions of your site. Perhaps an admin site vs a user site, or a mobile vs regular browser site, for example.
The pro/con question is a larger topic up for debate as depa mentioned. However do note that javascript+css files are typically small, gzip-friendly, and with the asset pipeline, cache-friendly. In short, you probably don't need to optimize this unless you have a very specific need.
Upvotes: 3