0

I have an app on Heroku written in Rails which has a big image on the landing page as background. As Heroku's file system is read-only, I decided to store these images (they are randomly selected) on AWS S3.

My code in the .css(.scss) looks like this (simplified):

html {
  background: image-url('#{image}.jpg') no-repeat center center fixed;
}

My problem now is that my app is able to display the image correctly in development, but it is no able to display the image in production (on Heroku). When looking at the delivered .css file (in the browser), I think I see the error:

html {
  background: url("8017416067_4f6f75f956_o.jpg") no-repeat center center fixed;
  -webkit-background-size: cover;
  background-size: cover;
}

It seems that the hash is not appended, although I use image-url, as seen above and as suggested i. e. here. The filename the file has on S3 is 8017416067_4f6f75f956_o-058c92aeab457efe0625a777f203430d.jpg.

Any suggestions on what I am doing wrong here?

EDIT: I noticed that in the .css files I get from my local machine, the image is referenced with /assets/8017416067_4f6f75f956_o.jpg (without quotes), while the online app gives me "8017416067_4f6f75f956_o.jpg".

SOLVED: I don't know why, but suddenly it works. The last thing I did was that I deleted the whole public/assets folder in order to make Heroku precompile the assets itself.

Community
  • 1
  • 1
BlackMamba
  • 1,449
  • 2
  • 12
  • 18

1 Answers1

0

I had the same problem in production env once - URLs were not appended with hash. The reason was that my *.css file was not compiled for production environment, so the website was using the original *.css file with "unhashed" URLs in it.

First, check if you see in browser compiled css i.e. it's name is appended with hash. In case if uncompiled *.css file is used you could check whether it is included in the list of "compilable" files in config/environments/production.rb:

config.assets.precompile += %w( mystyles.css )

The Asset Pipeline: Precompiling Assets

One more reason I see may be that the compilation was run with the wrong environment (not for production). Use the following command on your server:

RAILS_ENV=production bundle exec rake assets:precompile

But I think Heroku already runs this command by default, mentioning just in case.

Michael Radionov
  • 12,859
  • 1
  • 55
  • 72