Rails asset pipeline on staging: correct fingerprint but 404ing
Asked Answered
P

5

9

I'm running Rails 3.1.3, which includes Sprockets 2.0.3 as a dependency.

I set up my staging environment to be configured the way the Rails guide suggests for production.

config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true

I have included in my Capfile

load 'deploy'
load 'deploy/assets'

And assets get precompiled as expected on deploy.

In public/assets, I find the assets as expected with fingerprints.

application-bd402855d34fb61e0a1690da06f79f20.js
application-bd402855d34fb61e0a1690da06f79f20.js.gz
application-ed3f9a8d23992790841c11b6692fb576.css
application-ed3f9a8d23992790841c11b6692fb576.css.gz
...and a bunch of images...

When I load the page, I see the correct references, fingerprint and all.

<link href="/assets/application-ed3f9a8d23992790841c11b6692fb576.css" media="screen" rel="stylesheet" type="text/css">
<script src="/assets/application-bd402855d34fb61e0a1690da06f79f20.js" type="text/javascript"></script>

However, everything 404s, css, js, images, everything.

Anyone know what the deal is here? Thanks!

Pistoleer answered 8/12, 2011 at 2:33 Comment(8)
Are you running staging as 'production' or do you have a staging.rb config file? If so, you may not have the correct pipeline options in there.Prase
Staging is set up with the config options above, which is the same as what the Rails guide suggests for production. I want pretty much the same behavior. Is that not right?Pistoleer
It should be fine. I will think about this some more...Prase
I have the same issue in production mode. Tested locally with exactly same version of rails ruby and sprocket.Tombac
Have you tried doing a rake assets:clean before your precompile? Your staging environment should be identical to your production environment, as this is the whole point of staging.Description
Have you read this: #9437952Shaia
Does your application run at a subdirectory?Monomorphic
Did you configured your Web server (e.g. Nginx) to serve your static assets ?Harsho
N
1

If you are certain the assets are being compiled and exist in the public directory, could it be your web server settings? On production/staging environments the assets shouldn't hit the rails app and be served directly from the web server. Heres an example apache config snippet:

   <LocationMatch "^/assets/.*$">
      Header unset ETag
      FileETag None
      # RFC says only cache for 1 year
      ExpiresActive On
      ExpiresDefault "access plus 1 year"

      SetEnv no-gzip
      RewriteEngine on
      # Make sure the browser supports gzip encoding before we send it
      RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b
      RewriteCond %{REQUEST_FILENAME}.gz -s
      RewriteRule ^(.+) $1.gz [L]

   </LocationMatch>

   <FilesMatch \.css\.gz$>
      ForceType text/css
      Header set Content-Encoding gzip
   </FilesMatch>

   <FilesMatch \.js\.gz$>
      ForceType text/javascript
      Header set Content-Encoding gzip
   </FilesMatch>
Nonrecognition answered 23/8, 2013 at 6:20 Comment(0)
D
0

Despite suggestions in other answers

config.assets.compile = true

...is a workaround, not a solution. This option enables Rails to fall back to on-the-fly compilation of assets that can't be found in public/assets. It may 'solve' your problem in staging but having Rails compile assets at run-time is not exactly optimal in production.

I remember in the early months of working with the new asset pipeline in Rails 3.1.x that I had problems with both compression and generation of digests that I only really solved in later versions. I'd suggest trying out

config.assets.compress = false
config.assets.digest = false

both individually and together. And/or upgrade to later versions of Rails or the asset pipeline gems.

Dukas answered 13/3, 2013 at 11:4 Comment(0)
A
-1
config.assets.compile = false

Should be:

config.assets.compile = true

Also, make sure clear your cache:

bundle exec rake tmp:cache:clear

and restart the server.

Aleppo answered 20/3, 2012 at 19:14 Comment(0)
S
-1
config.assets.compile = false

It should be true

Sypher answered 13/3, 2013 at 6:46 Comment(0)
D
-1

I ran into the same issue a few months ago. For a few reasons I chose to go with manually triggering the asset compilation in production so my production.rb has

config.assets.compile = false

and by capistrano deployment also has a task to precompile the assets (using rvm as well):

run "cd #{release_path} && RAILS_ENV=production bundle exec rake assets:precompile", shell: fetch(:rvm_shell)

The ultimate step was making sure that we symlinked the assets folder so that we don't need to recompile assets that have not changed.

run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"
Dimer answered 14/8, 2013 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.