Jimmy Chu
Jimmy Chu

Reputation: 982

Configuring Rails Asset Pipeline in dev and production

I am still not quite sure about the best practice of using rails asset pipeline. In a rails skeleton project, the application.html.erb contains code as:

<head>
  <title>My title</title>
  <%= stylesheet_link_tag "scaffolds" %>
  <%= stylesheet_link_tag "depot", media: "all"  %>
  <%= stylesheet_link_tag "application", media: "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>

Now in development, all js and css would be served properly. But in production, after precompile all the asset (js and css), I only need application.css and application.js, as scaffolds.css and depot.css are concatenate in application.css. So scaffolds.css and depot.css are no longer needed in production.

How could I have the above code in dev and then in production, have asset pipeline know certain assets have been concatenated in application.css/js and then remove those asset link tag in the code?

If this is not possible, how should I avoid loading redundant resources and keeping prod deployment manageable? (meaning no deleting the link_tag manually in production)

Thanks.

Upvotes: 0

Views: 299

Answers (1)

deefour
deefour

Reputation: 35360

You do not put

<%= stylesheet_link_tag "scaffolds" %>
<%= stylesheet_link_tag "depot", media: "all"  %>
<%= stylesheet_link_tag "application", media: "all" %>

in your layout file. You only put

<%= stylesheet_link_tag "application", media: "all" %>

And in config/environment/development.rb you make sure you have

config.assets.debug = true

When config.assets.debug is set to true it will automatically generate the <link ... /> tags for each of your stylesheets in dev mode for each assets required in the application.css file's Sprockets directives.

In config/environment/production.rb config.assets.debug will be false by default, causing the assets to be concatenated together into a single application.css file (again, assuming your //= require lines are correct in application.css). This will cause 1 link tag to be created for only application.css in production.

Upvotes: 2

Related Questions