wintermeyer
wintermeyer

Reputation: 8318

Inline CSS from the asset pipeline

A fresh Rails application has the following code in app/views/layouts/application.html.erb to include a link to the - in production - minified CSS:

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

For the reason of WebPerformance I'd like to use the asset pipeline to minify and organize my CSS but inline it in the header.

How can I tell Rails to inline the CSS of the asset pipeline in the application.html.erb?

Upvotes: 2

Views: 951

Answers (1)

wintermeyer
wintermeyer

Reputation: 8318

This code will do the trick:

<style>
  <% css = File.read("#{Rails.root}/public#{asset_path("application", type: :stylesheet)}") %>
  <%= css.html_safe %>
</style>

Upvotes: 2

Related Questions