Wednesday 26 June 2013

Rails 4 Asset Pipeline Gotcha

Last night, I upgraded a site to rails 4. The site is a pretty basic rails app, so the upgrade path wasn't too painful, pretty much all I had to change was to do with the new mass assignment security - whitelisting of form params and the controller rather than the use of attr_accessible on the model.

The upgrade went smoothly until I pushed to heroku, the app built and ran successfully, however, a javascript plugin I was using wouldn't load in production. The javascript plugin is CKEditor, which has a number of javascript and css files which are loaded by including a script tag to just one root javascript file.

When using rails 3, I had placed the ckeditor folder in 'vendor/assets/javascripts' and forced precompilation of the folder's entire contents by adding config.assets.precompile += [ 'ckeditor/*'] to production.rb. This worked fine, as the asset pipeline compiled 2 copies of each file, one with an added hash in the file name and a version with the unaltered original filename.


In Rails 4 this no longer works, the asset pipeline no longer compiles 2 copies of the file, it only compiles the 1 copy adding the hash to the filename or not dependent on the config.assets.digest option. Now I don't want to disable the fingerprinting of my javascript and css, I want the caching benefits this gives me. The solution was therefore to disable the precompilation of the CKEditor files, and move the CKEditor folder to the 'public/assets/' folder.

Try where possible to include 3rd party javascript and css in the asset precompilation, however for those plugins such as CKEditor which will not work with fingerprinted file names, this technique offers a get out of jail card, but remember, these files have the potential to get stuck in caches and as the filename doesn't change when you deploy a new version of the plugin in may take a while for all clients to get the latest version!