Geoff
Geoff

Reputation: 9570

Ruby on Rails caching images bug

With Ruby on Rails 3.1, I have some JPGs in the following directory:

assets/images/subdir1/subdir2/myimage.jpg

Those images are loaded dynamically via JavaScript in the website (img.src = "assets/subdir1/subdir2/myimage.jpg"). For some reason, they are not getting cached in production, and instead being served with cache-control: no-cache.

How can I ensure these images get cached?

In my production.rb file I have something like this:

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  config.assets.precompile += ['jquery-1.8.3.min.js', 'json2.min.js']

Figured that images would be cached already based on that comment.

Edit with more information:

The JavaScript is custom JS that I wrote. Basically, I have a custom JS project that builds and copies itself into the Ruby on Rails directory. Inside this custom JS, I do something like img.src = "mydomain.com/assets/subdir1/subdir2/myimage.png"; This works, however the image doesn't get cached by RoR. Maybe there's a way to tell RoR that every image found recursively in subdir1 should be cacheable? – Jeff just now edit

Another edit:

Maybe I can write my own handler that will serve the files and say they should be cached?

Or put a special config file in their folder (like .htaccess for Apache servers?)

Surely there's a way to do this...

Upvotes: 3

Views: 1504

Answers (3)

irfn
irfn

Reputation: 560

Normally in production like environments, you would use something like apache/nginx etc to serve static assets. If you want the rails app to serve static content, then you need to enable it in config

config.serve_static_assets = true
config.static_cache_control = "public, max-age=172800"

This is discussed here in this thread as well. Setting Cache-Control headers on js assets in RAILS 3.1

Upvotes: 0

David Grayson
David Grayson

Reputation: 87386

What kind of caching, exactly? If you want to change the http headers the images are served with, then this is really a question about how to configure your web server (Apache).

If you want them to be cached by rails in the public/assets directory, I think you should run the asset precompile rake task when deoloying, though maybe it's not necessary.

Upvotes: 1

Fa11enAngel
Fa11enAngel

Reputation: 4810

You have to use the helpers of rails to serve the files. Only then rails can say whether this file has changed or not. If you use the helpers, Rails knows which files have changed and servce the right path. Try this:

# access to asset_path through helper.
img.src = "<%= asset_path('subdir1/subdir2/myimage.jpg') %>"   

If you use the asset pipeline the right way, rails will generate a new path to the file on file changes and you won't fight with new pictures lying behind the same url.

There are examples, see the Ruby on Rails Guide for Asset Pipeline for more information. See paragraph 2.2.3 JavaScript/CoffeeScript and ERB.

Upvotes: 3

Related Questions