Rails asset pipeline: Why things break in production and what precompilation does to your assets
When you work with a Rails 3.1+ application, you will be working with the asset pipeline. The asset pipeline is awesome until you deploy. Then it will be less so if you haven't done everything as the pipeline expected it from you.
The problem
When using the asset pipeline your assets (images, javascripts, stylesheets, fonts) live in folders inside
app
:
With the asset pipeline, you can use the full power of Ruby to generate assets. E.g. you can have ERB tags in your Javascript. Or you can have an ERB template which generates Haml which generates HTML. You can chain as many preprocessors as you want.
When you deploy, Rails runs
assets:precompile
which precompiles all assets into static files that live in public/assets
. This way you have all the performance of static files with all the expressiveness of Ruby.
Unfortunately, the path of every single file changes during precompilation. This means that you have to be a bit careful how you reference assets.
won't work any longer, because there is no
public/assets/images
. foo.png
now lives directly in public/assets
.Example
This precompiles into this (fingerprinted and compressed files removed for clarity):
What has happened?
- All asset folders (
app/assets/images
,app/assets/stylesheets
,app/assets/fonts
, etc.) are merged into one folder:public/assets
. - Subfolders are also merged into
public/assets
. That means bothapp/assets/javascripts/subfolder/*
andapp/assets/stylesheets/subfolder/*
now live inpublic/assets/subfolder/*
- All CSS files are concatenated into one file (
public/assets/application.css
). With the default manifest inapp/assets/stylesheets/application.css
, stylesheets inapp/assets/stylesheets/**/*.css
are concatenated. Sopublic/application.css
has the following content: - All Javascript files are concatenated into one file in root (
public/assets/appliction.js
). With the default manifest inapp/assets/javascripts/application.js
, Javascripts inapp/assets/javascripts/**/*.js
are concatenated. Sopublic/application.js
has the following content:
What will break after precompilation?
- Stylesheets, that used
url(../images/foo.png)
-style tags - @font-face definitions that used
url('fonts/bar.ttf')
-style tags
How can I fix my stylesheets?
- Do not use
url(../images/foo.png)
.
This is plainly wrong, since from your browser's point of view, your stylesheet lives at/assets/application.css
and your image at/assets/foo.png
. I have no idea why it works in development in the first place. Simply use the SASS helper:
How can I fix my font definitions?
- With the Asset-Pipeline fonts are stored in
app/assets/fonts
.
Depending on your rails version, you may need to addto your config/application.rb - Do not use
src: url(...)
in your @font-face definitions.
Again instead use a sass-helper:src: font-url(...)
- Maybe the referenced paths need adjustment. If before it was
src: url(fonts/...)
now the leadingfonts/
directory needs to be removed, because the fonts are compiled to the assets folder by the asset-pipeline.
https://makandracards.com/makandra/8951-rails-asset-pipeline-why-things-break-in-production-and-what-precompilation-does-to-your-assets
No comments:
Post a Comment