简体   繁体   中英

How to inline compressed CSS in Rails with assets pipeline

I'm trying to inline CSS into my layout. I'm currently using

= Rails.application.assets.find_asset('embedded.css').body.html_safe

However, the CSS returned is not compressed . I verified what .digest_path asset file exists, and is properly compressed.

I can, of course, write a helper that will check if current on-disk compressed asset file exists for a given asset, and use it. However, I think find_asset actually compiles a CSS asset each time it is called -- not good in production. I hope a cleaner solution exists for this issue.

If assets are to be rebuilt in Production, production has to have access to minifiers. By default, Gemfile has something like this:

group :assets do
  gem 'sass-rails'
  gem 'coffee-rails'
  gem 'therubyracer'
  gem 'uglifier'
end

Need to remove :group assets do , so these gems are included in production environment too. Then, find_asset rebuilds the asset and minimizes/compresses it on the first call, and caches it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM