rails page caching dynamic images

My exchange rate graph application dynamically generates graph images, but the data only change once per day, and it always shows the same default image when you open the page. So there is clearly some gain to be made from caching the images.

I got quite confused and spent a while fiddling with various Rails options and methods to get page caching to work for the images. The answer turned out to be much simpler though!

Rails can do page caching of images using the caches_page class method, without any extra work, but you have to ensure that the image URL that is requested has the correct extension. If there is no extension, it defaults to being treated as .html.

In my application the image URL is created programmatically, either in JavaScript or the controller code, so I just had to change that to append .png. If you use url_for or equivalent, you might need to make sure your routes.rb are correct.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*