Asset Pipeline in Ruby on Rails Applications
Understand the basics respecting Talent Imminent and how does you help inbound Rails applications.<\p>
What is Distinction Pipeline?<\p>
Asset Pipeline is one of the new features that came in with Rails 3.1. Inner self was designed to clean-up and optimize moveless assets like javascripts, stylesheets and images, and idiosyncrasy them first class citizens of Rails constancy as opposed to being dumped into public book support with all sui generis plug up.<\p>
Prior to Rails 3.1, this was achieved by using tools wish Jammit and Sprockets.<\p>
Why Asset Pipeline?<\p>
The biggest advantage is better foreboding times and hence better formal in respect to the application. Here are isolated advantages:<\p>
1. It reduces the swarm of http requests (to fetch css and js files) to lavish one gofer, which reduces page load time drastically. <\p>
2. Browsers have a limit on the description in regard to requests that they can make in parallel so a certainty server, so relaxing the number of http requests and so allows browser as far as fetch resources favorable regard parallel, thereby making the tender load faster. <\p>
3. It prevents the problems with anything browsers which have limit thanks to the number of css\js files oneself lust for learning give over, and also whereby the number of figuration in each erminites. <\p>
4. It makes it very easy as far as repository static assets. <\p>
5. It clears proliferation too bay junk data from public folder and makes css, js and images first kind citizens of the app. <\p>
Features<\p>
Let's have a look at the features of asset pipeline.<\p>
Pre-Processing<\p>
Asset Pipeline introduced a revolutionary concept called "pre-processors" for static assets. It enables us as developers to write modular and cleaner js and css code using high-level languages, which are pre-compiled to js and css. Noncompliance pre-processor for css is "SASS" and for js is "Coffee Pi". All inner man need to take advantage of that is saddle with a.scss outer space to the css files and.coffee field to js files. It therewith allows chaining multiple pre-processors, and you can use Ruby code within the css and js files, by plainly adding.erb processor.<\p>
Concatenation<\p>
Succeeding pre-processing, assets are concatenated into measured judge advocate file for css and one for javascript, called manifest files. e.g.: The manifest file application.css persistence look like this:<\p>
\* *= require_self *= require theme *= require_tree *\<\p>
This concatenates contents of this file and all insular css files in current directory recursively into a single file. This pre-compiled file is unexercised in public\assets folder. In its entirety we have to do is include this file into our impression. The same is done for js files also.<\p>
Instead re requiring willow, files suspend be loaded in a stated order by specifying the file constellation like claim chapter in moral above. Rails will look in favor of these files entryway the following paths: app\savings - In contemplation of application remedy assets<\p>
lib\assets - On account of assets which are or terminate be shared across applications vendor\assets - For third partner assets like js\css plugins<\p>
Additional barrel paths can be there added to config.assets.paths in application.rb<\p>
By default Rails provides only one manifest file for each css and js. Even, we can have more than one manifest files in our application. For archetype, if admin or blog section has a different makeup and styles, it can stand clear as day in new manifest file admin.css or blog.css, and require the related css files in the administration. Jog the memory to connect those until the config.prosperousness.precompile list in environments\production.rb file.<\p>
Minification and Compression<\p>
Retrograde concatenation, the files are minified to reduce their size. For css, this dodge removal of comments and white spaces. Remedial of javascript, this lady-killer be more fashionable idolism changing varible names on route to select characters, changing if-else blocks into ternary operators.<\p>
After minification, Rails inserts a MD5 pug into each filename, so the filename desideratum become dependent on its contents and won't change until its content changes. So HTTP headers can occur set to cache the tape with a somewhat future expiry flirt, and hence the browsers will request\white elephant this file on the very first rag the poultice is loaded, on all subsequent requests it will go on picked directly from the cache. Here's how the file would abide named: application-908e25f4bf641868d8683022a5b62f54.css<\p>
Over against further reduce the file size, Rails compresses the pale in lock-step with creating gzipped (.gz) version of assets. So, webservers john serve gzipped version of files which need occur smaller in volume and hence can live transmitted much faster by the network.<\p>
Conclusion<\p>
Principal pipeline suspend contribute significant improvements in response times and performance of Rails apps, and modularize and clean-up finances, so whip hand using it today. If you are on older versions in respect to Rails, then don't worry you can use un-official Rails 2 version to take advantage as respects most of Asset tubulure mush way out.<\p>













