mc6e
mc6e

Reputation: 1

Why updating a static file via file upload does not get reflected when using Rails asset pipeline?

I have a Rails application with asset pipelining turned on. The application allows to upload logos, which need to be served as static assets. The logo file is a column in a table. In general this works fine both in development & production mode. Both the files from app/assets (respectively public/assets) and the uploaded logos located in public/logos are correctly served to the browser.

But in case a different logo using the same file name is uploaded, the browser still shows the old version (both in development & production mode). But when I clear the browser cache, it get's updated. I guess this must be related to caching. I assume there is a way to outdate the cache for the updated file, but can't figure out how. Neither do I want to enforce users to use a different file name in case they want to upload a modified version of the logo. Nor is it feasible to ask users to delete their browser cache. Using the assets:precompile task is no solution as logos are added during server runtime.

The asset pipeline together with caching is still a bit mysterious to me although I was using it for a while. Has anyone else seen this behaviour?

Running on Rails 3.2.13 with Ruby 1.9.3.

Upvotes: 0

Views: 99

Answers (1)

davidfurber
davidfurber

Reputation: 5528

This doesn't sound like an asset pipeline problem.

I assume that the logos are saved using a directory structure like /logos/:id/filename.png, and that the problem happens when the user replaces their logo with a different one, and that it's using the same id rather than creating a new one.

I would either have it replace the logo record so you get a new ID, or rename the uploaded file to something like ":filename_:timestamp" so that the filename changes and invalidates the browser cached version.

If you are handling the file uploads yourself rather than using Paperclip or Carrierwave, then it should be just as easy to do this by renaming the file before you save it to public/logos and writing the filename column in the database.

Upvotes: 0

Related Questions