How to inline stylesheets and javascript in Rails view

less than 1 minute read

Derek Sivers once mentioned that he made his website a bit faster by inlining the stylesheets in a <style> tag instead of pulling it from a css file which would’ve costed another network request. You can see it by viewing source of any of his pages. You can ‘View Source’ any page in his website and notice it.

Let’s see how to do it in rails.

For stylesheets you replace

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>


  <%= Rails.application.assets.find_asset('application').to_s.html_safe %>

You could do the same with javascript if needed.