The Rising Sun Blog

News, announcements and thoughts of the Rising Sun Team.

Also includes random musings about Ruby on Rails, WordPress and life.
Image
Sep
08

Seemed like a totally innocent thing to do. Serve out static javascript files directly from the filesystem by Nginx.
We put the following rule in our nginx file

  # Add expires header for static content
  location ~* \.(js|css|jpg|jpeg|gif|png)$ {
    if (-f $request_filename) {
      expires 5d;
      break;
    }
  }

This would serve our cache our javascript/css etc predominantly static files. Nice!
But, we use AJAX with Restful routes such as formatted_users_path(:js).
This results in a nice looking URL ending with a .js.
Ouch!
Things would work in development but strangely we’d get Nginx 404s when we deployed.
The workaround was to either not use formatted js paths in our code (and let rails detect AJAX calls as using the xhr? method) or we fix our Nginx configuration to not cache .js files.
Use as see fit in your situation. I’d rather let my javascript be cached if i’m not going to have a AJAX heavy site.

blog comments powered by Disqus