Get Rails pipeline to gzip xml assets - ruby-on-rails

I'm using the rails asset pipeline for my rails site. The assets:precompile task properly processes and creates gzip versions of my css and js files automatically.
I've now added some xml assets. The asset pipeline lets me reference the files just as I do for binary files like images. However, the precompile task does not create gzip versions of the xml files even if I explicitly put them in "config.assets.precompile". My xml files are large assets and would get about 3 times faster with gzip, so this is an important optimization for my site. Is there any way to configure the pipeline to gzip files with additional extensions like ".xml"?
Notes:
I'm using the asset_sync gem to move all my static assets to the
CloudFront CDN, so I can't rely on my webserver (NGINX) to
automatically gzip the files for me. asset_sync will upload a
gzipped version of a file if the asset-pipeline created one, but it
won't create gzipped versions for you. I could theoretically fork
asset_sync and hack it to gzip xml files before the upload or to
insert an additional task between precompile and the sync tasks, but
I would prefer to avoid maintaining a fork of the gem.
I've also thought tricking the asset pipeline by using a .js
extension instead of .xml, but I'd prefer a less hacky solution.
If there's no clean way to just configure gzip on an additional file
type, is there a way to add a custom "postprocessing" filter on files
where I could add gzip manually? I've seen guides on how to add
custom preprocessors but not a post processor on the final hashed
asset file.

This option was added to Rails 4. XML is compressed automatically and Rails exposes a configuration option, zip_files, for specifying which file types to compress.
https://github.com/rails/rails/commit/7cfd1bd76a41bea528c945d821a9fbc3902af479

Related

How does Rails asset pipeline do preprocessing?

In this blog, it says that assets pipeline will compile, compress, and preprocess your assets from your app/assets and then placed the out into public/assets. I don't understand the below where it says you can set up a Nginx or Apache server to preprocess your assets for you after the it already has been outputed to the public/assets. Is it saying that you can choose to have additional servers such as Apache/Nginx to do the preprocessing for your while the Rails server does the compiling/compressing?
bundle exec rake assets:precompile
This will create (by default) an assets directory in your public/
folder. It will then add all the compressed and compiled files into
that directory, in the appropriate formats and with the new digested
versions. You can then set up Nginx or Apache to server those files
directly so that Rails doesn’t have to deliver them (and run the
on-the-fly preprocessing, etc.) itself.
I don't understand the below where it says you can set up a Nginx or Apache server to preprocess your assets for you…
It doesn't say that. It does say the following:
You can then set up Nginx or Apache to server [sic] those files…
There's a difference between preprocessing and serving files. What this documentation is suggesting is that if you're using a server like nginx, you can configure it to handle requests to assets that exist in your public/assets folder. This alleviates your Rails app from handling those requests.
Pre-processing is still handled by Rails in advance of nginx coming into the picture.

Ruby on Rails - setting specific path to directory in rails app

Hello I am attempting to implement Trumbowyg emojis plugin in my ruby on rails application. So I have referred to the below link for guidance on how to implement the plugin.
https://github.com/Alex-D/Trumbowyg/blob/master/examples/plugins/emoji.html
$('#editor').trumbowyg({
btns: [['emoji']]
});
function initEmoji() {
emojify.setConfig({
img_dir : '../../bower_components/emojify.js/dist/images/basic/',
});
emojify.run();
}
$('.trumbowyg-editor').bind('input propertychange', function() {
initEmoji();
});
initEmoji();
How do I store the images in a directory and make reference to the directory under img_dir (as shown above) in a rails app?
Option 1: Embedding into Asset Pipeline
Without knowing exactly how this Javascript library works, the image path needs to be added to the Asset Pipeline. Otherwise rake assets:precompile will not include all of the emoji images in the manifest file it generates and will not know how to serve those assets in the production environment (assuming default Production environment configuration).
See #config/initializers/assets.rb
# Add additional assets to the asset load path
Rails.application.config.assets.paths << Rails.root.join('path', 'to', 'emoji', 'image', 'directory')
Option 2: Serve static images from /public
Another option is to not serve these files from the Asset Pipeline at all. It may be too much trouble to coerce the Asset Pipeline into serving these assets correctly without modifying the Javascript library to use asset-url in all the JS and CSS files.
Instead, copy the files to a directory under /public so the Rails app can serve them. Then adjust the img_dir in your Javascript configuration to be the path where the files are in /public. Ex: /emoji_images if you put the files in /public/emoji_images.
Depending on how you're hosting the app, you may have to configure serve_static_files in config/environments/production.rb.

How to avoid GZip compressed assets getting modified every time running rake assets:precompile

We are using Rails' asset pipeline feature in v3.2. We are pre-compiling all assets locally and committing the pre-compiled assets into our Git repository. Every time we run rake assets:precompile all the GZip compressed versions of the generated assets in public/assets/ are marked as modified. I suspect this is because the GZip file format contains a header which includes a version number and a timestamp.
What's the best way to avoid this issue?
We have considered:
Using the turbo-sprockets gem to compile only assets that have changed. This seems the best option so far.
Precompiling on the target servers instead of locally. Makes deployments take significantly longer, esp. with frequent deployments and infrequently changing assets.
Not precompiling the GZip compressed assets and instead having the web server compress the static assets on the fly. Adds extra load on the web server and also Sprockets is supposed to use a higher compression factor than the web server leading to longer downloads.
Any other options?

Why three assets directory generated in rails 3.1 and higher

Can anyone explain what the use of the following directories is?
app/assets/
lib/assets/
vendor/assets/
These directories are all a part of Rails' Asset Pipeline.
Conceptually, the app/assets directory is for your application assets (for instance, the stylesheets and images for your application). lib/assets is for all of the code that you've written that stands alone from your rails app (javascript library, maybe). vendor/assets is meant to house all third party libraries (e.g. jQuery).
All of these paths are included by default in the asset pipeline. This means that their contents can be included into other files using sprockets, concatenating automatically into one file (javascript or css), reducing the number of requests and thus the loading time. The asset pipeline can also compile your coffeescript and minify your javascript for production.

What is the best place to store static assets (files) in Rails ( pdf forms, xls files, etc)

I have a bunch of static assets ( not jpg, css, & js) - rather files like pdf forms, xls that I need to serve to users. They rarely change. Before I used to store them in public folder, but with the introduction of the assets pipeline in rails 3.1 what is the best place to store files like that now now?
Actually I just tested it by creating a folder in app/assets/files and sticking my xls files in there, and rake assets:precompile task just picked it up.
Also this needs to be added for Rails < 3.1:
# Enable the asset pipeline
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/files"
The best place for items like this is still in the /public directory, remember to have your web server serve these assets directly also.
The assets directory is only needed if you want to take advantage of the asset pipeline. The asset pipeline handles things from compressing and compiling .coffee and .less or sass files to compressing your js and css into one file so your webserver only has to serve one file for each.
When you compile your assets with the rake task bundle exec rake assets:precompile they are moved to your public directory anyhow.

Resources