Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails asset pipeline on staging: correct fingerprint but 404ing

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!

like image 419
cotopaxi Avatar asked Dec 08 '11 02:12

cotopaxi


2 Answers

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.

like image 186
Mark Weston Avatar answered Oct 18 '22 02:10

Mark Weston


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>
like image 38
stefanlz Avatar answered Nov 19 '22 22:11

stefanlz