I'm using carrierwave to allow users to upload images with an ability to crop after upload, the issue is the versions get created and CDN catch them instantly that when users crop they don't see changes because the Cloudfront cache has to be invalidated and takes quite a decent time to refresh on its own.
I thought putting version numbers on file names might be a good way to sorta invalidate cache, how to achieve that using Carrierwave?
And is that the best approach?
Object invalidations typically take from 10 to 100 seconds to complete. You can check the status of an invalidation by viewing your distribution from the CloudFront console.
The Invalidation allows us to remove object(s) from the Cloudfront cache before it expires. It allows you to remove a specific object from cache as well use supported wildcard character to remove multiple objects. You can also remove all the objects from cache by using “/*” parameters to invalidation requests.
I had the same issue with cloudflare
here my hackish solution: put the updated_at timestamp in a params. Should work with all CDN
class AssetUploader < CarrierWave::Uploader::Base
def url(options={})
super.split("?v=")[0]+"?v=#{model.updated_at.to_time.to_i}" rescue super
end
end
To make the url trick work on Amazon CloudFront CDN, just remember to enable query string in your distribution. Go to the behavior tab and enable 'forward query strings'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With