I’m developing a webpage, which content is fully generated with client-side JavaScript. The only purpose of index.html is to refer to JavaScript and CSS documents, which are generated with Rails’ asset pipeline. To avoid extra requests, I’d like to inline those JavaScript and CSS in production.
How to inline JavaScript and CSS content which is generated with asset pipeline?
You can use
Rails.application.assetsto get the content of the assets after precompilation. Add this in yourindex.html.erb:Don’t forget the
raw, otherwise"will be changed into"etc.Note this isn’t compressed; for production, you will probably want to inline compressed code. To do so, add
gem 'uglifier'to your Gemfile, run bundle, and use this: