-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assets:precompile doesn't replace image asset paths in CSS #1209
Comments
You have to use ERB inside your CSS to fix this. You can do this by appending .erb to your css/scss file:
Then you have the body {
background: #00ff00 url(<%= asset_path 'rails.png' %>) no-repeat fixed center;
} |
I added |
Hashes are only applied in production. Running |
Sure enough, thanks! |
So there is a workaround but this is still a bug? Either CSS assets shouldn't be handled by the asset pipeline at all or the correct thing should happen when you compile the assets. |
Everything @MSchmidt said is correct. If you don't want images to be handled by the asset pipeline, just keep them in public/images as you had them before. |
I can understand what you're saying but this fails the principle of least surprise. A new developer comes along and is told about the asset pipeline, you can use it in your css - this is great. However, when you deploy to production it will all break! Either you need to make this very clear in the documentation (ie. don't use it) or don't use the asset pipeline for css assets in development. Just my thoughts. |
I agree with @moomerman, this is really confusing. |
Agree with @moomerman; maybe it's low priority to fix, but this behavior is definitely worse for the user |
This issue tripped me up when playing with 3.1. I do agree that the default behaviour should be more newbie friendly... |
The problem aren't the hashes, the problem is the different behavior between dev & production. Why don't you apply hashes in development as well? Unless you have hundreds of Megabytes of assets, it'll take fractions of a second to get the MD5 sums. |
+1 |
Agree with iGEL, seems like a feature that is meant for production shouldn't need me to be in production environment to use. The main reason this is a fail is because there are several things that I only use in production/staging which I don't want to setup locally... Memcached being one of them.
|
@MSchmidt & @josh, I am currently testing this using 3-1-stable branch and it is not working as described. I get assets with no hashes. Not sure what is missing here but I have been chasing my tail trying to make sense of the asset pipeline with nothing more than frustration for the last week. I remember this working in beta1 (will have another look), not sure what happened along the way. Where is the outline for expected/intended behavior that was used to implement this feature so we can get some idea of how it is suppose to work without sifting through 2 plugins and rails core? |
The pipeline should handle the CSS files correctly by default or the CSS files should be treated as ERB by default. |
So, if I'm following along correctly, using Rails 3.1 asset pipeline set up as it is by default – aka using |
@contentfree The asset will be served but not how you expect it to. If you're running a web server on the front end then the web server won't serve the asset, it will fall back to the application and get served by the asset pipeline as in development. This is confusing though since you're precompiled your assets so you expect them to be served from the precompiled cache (and by your web server if you have one). Another issue I just noticed is that it is serving the static asset via the asset pipeline in production even though in production.rb I have:
|
@moomerman can you verify that it works as described by @MSchmidt on 3-1-stable? From my end it does not. |
@lardawge I just tried against the latest 3-1-stable and got an error while running RAILS_ENV=production rake assets:precompile
|
Here is a pull request to fix that: #1387 You can try my fork until this gets accepted... So for me with 3-1-stable, Expected behavior, @josevalim can you give us a hand sorting this... I am happy to do some work to make the required changes and get it working, just need to know the intended behavior if it differs from what I suggested. Also can we reopen this ticket? |
Ok, sorted out some of the issues that I have been having.
Rake::Task["assets:precompile"].clear
namespace :assets do
desc "As if production env"
task :precompile => :environment do
# Give assets access to asset_path
ActionView::Helpers::SprocketsHelper
assets = Rails.application.config.assets.precompile
Rails.application.assets.precompile(*assets)
end
end I was able to run As an aside, the api will change to the following for minifying js/css in 3.1. This caught me off guard because there was no mention of the change other than looking through the source code. You will also need to add # development.rb
if $precompile_assets
config.assets.compress = true
config.assets.js_compressor = :uglifier
config.action_controller.perform_caching = true
else
config.action_controller.perform_caching = false
end #assets.rake
namespace :assets do
desc "As if production env"
task :precompile do
# Set global to compress and fingerprint assets
$precompile_assets = true
# Load rails
Rake::Task['environment'].invoke
# Give assets access to asset_path for fingerprinting
ActionView::Helpers::SprocketsHelper
assets = Rails.application.config.assets.precompile
Rails.application.assets.precompile(*assets)
end
end Lastly, image_tag generate hashed urls if you only use the image name or relative paths. <%= image_tag 'rails.png' %>
<%= image_tag 'nested_folder/rails.png' %> Hopefully this is helpful to someone... |
fyi: in sass rails I will be adding native sass functions to ease access to asset paths.
will search the asset path for an image named foo.png and lastly it will look in public. |
@chriseppstein will this implementation be included in 3.1? |
@chriseppstein is there an issue on sass-rails that I can watch to track this (and possibly help test it out for you)? |
Guys The asset pipeline works fine for me in Staging/Production I think it's great... It's crippling me in Dev mode though... having to make a request to the pipleine for each image on my app means it now takes about 20-30 seconds for a page to load. Try styling a page with that? I'm going bald here guys, is there something I've missed? |
In the meantime: cp -R app/assets/images public/assets really helps! |
@Bodacious I had same issue, but problem wasn't in asset pipeline. I use mongoid wchich by default preloads all models in developemnt evnironment. After switching this off each asset load in aboud 100ms. You can find what is realy slow using rack-perftools_profiler. It helped me a lot. |
@MartinCui - thanks for the suggestion - I don't use Mongoid but I'll check out rack-perftools_profiler and see it I can find where I'm going wrong |
Don't know if it's possible, but would be neat if Sprockets could figure out that url("/assets/cat.jpg") (or a similar string in javascript) should be rewritten. I don't know the inner workings of Sprockets, but I'm thinking such a feature shouldn't be too difficult. Support for automatically converting small enough images into base64 would be neat too. That said, the new pipeline is great and this isn't a major issue. Using ERB or falling back to the previous behavior are both easy. |
The problem I'm having is that if I use "application.css.scss.erb" and then within that file want to import an SCSS partial (say _partial) then none of the following work: @import "partial" I need to be able to use asset paths within any SCSS and having to add multiple layers of parsing to each file (and still have it break) is entirely weird. Given that ERB tags are invalid SCSS, could we not pass all SCSS files via ERB and then hopefully the @import should work? |
Sorry, just realised there's a github user called "import" that will now be strangely pulled in to this thread. I was referring to the SCSS directive, not him. |
The @import (apologies to user named @import) is a SASS directive, not a sprockets directive. I think it would be cooler to include variables from other styles using the Difficult to do? On 28 Jul 2011, at 15:09, andyjeffriesreply@reply.github.com wrote:
|
Should start a new thread for this btw On 28 Jul 2011, at 15:09, andyjeffriesreply@reply.github.com wrote:
|
Huge +1 on that. Anyone with any decently DRY'd pattern of SASS can't really use the asset compiling because of this. |
only one comment to this new assets stuff: |
I just got bit by this issue when I tried to use an asset host. precompiling the asset gives me fingerprinted images which I then sync to my asset host, but anytime I call |
@subelsky please check that you don't have leading '/' in image-url value. If you use helper in this way: image-url('/logo.png') sprockets will not replace it with digest. |
Hi @alovak thanks for writing back; I tried that but it didn't work. Maybe because these files are in /vendor/assets/stylesheets? |
I also have files in vendor. But I include them with sprockets directive: *= require my-file this file is in /vendor/assets/stylesheets and it contains: ... the resulted css contains url("input-file/button-xxxxxxxxxxxxxxxxxxxxx.png") |
It worked! It turned out I had been writing my image-urls as |
Trying to get that doomed image fingerprinted in the css file. Following tips from rails/rails#1209. Signed-off-by: Zeljko <zeljko@zwr.fi>
In case anybody googles for this issue, I had this exact problem, and it turned out that the custom build script was failing to set RAILS_ENV=production. |
Still hawing this issue with: |
For me, fixed this using image-url(), font-url() properly. Only using url() did not help. This is a huge gotcha. Probably causing lots of people turn |
Since I was using |
This is with Rails3.1rc and beta1 If I have a background url specified in application.css like so:
The new asset pipeline stuff finds that image in the assets folder. However when I rake assets:compile the resulting compiled CSS still references rails.png but the asset is now called rails-9c0a079bdd7701d7e729bd956823d153.png so my production server wouldn't be able to serve that asset from the web server.
I would have expected it to expand it to:
The text was updated successfully, but these errors were encountered: