Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1.1 asset pipeline Heroku caching gotcha

The problem in a nutshell is that in development mode we'd make changes to CSS or JS files but would always get cached/old versions of these files. Nothing I did had any effect. I checked configuration dozens of times and tried every combination of config values but always kept getting the same results: stale/cached files. I had to actually run in production mode and restart the server after every change to test.

I spent days tearing my hair out over this issue, looked at dozens of stackoverflow questions on the asset pipeline but never found one that addressed it so I thought I'd post it here for posterity.

We use Heroku and precompile our assets because Heroku fails to precompile for us (we also use devise which apparently is the cause of the heroku precompilation failure). So in order to push our precompiled assets up to Heroku we have to check them in to git.

Here's the problem.

When we upgraded to Rails 3.1.1 asset precompilation produced files both with and without the MD5 hash in the name. I didn't think much of this and went ahead and checked all these files in so I could push to heroku. Sometime later I noticed the problem with cached results in development mode. The precompiled and checked in assets without the MD5 hashes were being served from /public/assets as static files which prevented us from seeing any changes we were making in /app/assets.

After finally realizing this I ran git rm /public/assets and everything works again. So the takeaway is: Be careful checking assets into git!

To turn this into a question: how do others do this? Am I missing something obvious? What I'd really like is for Heroku to precompile my assets for me but it is failing with a db connection error that I gather is because of devise. I had hoped Rails 3.1.1 fixed this but it didn't.

like image 971
Jeff Cutler-Stamm Avatar asked Oct 26 '11 07:10

Jeff Cutler-Stamm


2 Answers

Have you checked out this devise issue on github? Specifically Jose Valim says

Rails 3.1.1 final has a method called config.assets.initialize_on_precompile. If you set it to false, you should be good but it won't allow you to access model information on your assets (which you probably shouldn't anyway).

Maybe this will allow the precompile to happen on Heroku for you.

like image 109
Dty Avatar answered Nov 17 '22 11:11

Dty


The reason the asset precompilation does not work could well be, that the Heroku ENV vars are not present on slug compilation (deploy) as stated here:

http://devcenter.heroku.com/articles/rails31_heroku_cedar

There is an (experimental) way to enable the ENV vars during deploy for exactly this reason, find information here:

http://devcenter.heroku.com/articles/labs-user-env-compile

Hope this helps.

like image 1
Alexander Presber Avatar answered Nov 17 '22 09:11

Alexander Presber