3

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.

nycuser
  • 61
  • 2
  • Looking at the `write_asset` method in [Sprockets in Rails 3.2](https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/sprockets/static_compiler.rb), GZIPing is hardcoded to JS and CSS files only. Could you run a script to manually gzip the XML files before precompiling assets? – mccannf Jan 09 '13 at 09:43
  • Thanks for showing me the write_asset code. I can't pre-gzip the xml files because then the .xml and .xml.gz will get treated as two separate assets and be get hashed separately for caching. So, I'd have to do the gzip after the precompile and before the sync to cloudfront. – nycuser Jan 09 '13 at 14:56
  • 1
    Hmm. Rather than forking the whole `asset_sync` gem, could you create a new rake task [like the one from asset_sync](https://github.com/rumblelabs/asset_sync/blob/master/lib/tasks/asset_sync.rake), and before the `AssetSync.sync` command add in your own custom helper to gzip up your xml assets? – mccannf Jan 09 '13 at 15:13

1 Answers1

0

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