New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Active Storage - CloudFlare - Image Caching & CDN #9043
Comments
This is a nice solution for the X-Amz-Expires problem. Everything's work fine but When first time created It has X-Amz-Expires. Could anyone help this part? How to monkey patching with this? something like
Thank you! |
I had a similar need for a Rails app and this is my solution: https://stackoverflow.com/a/59107484/51387 |
For anyone else ending up here through Google, Rails now has supports proxying: https://github.com/rails/rails/blob/master/activestorage/README.md#proxying For Cloudflare you still need the configuration above. |
That worked for me https://andycallaghan.com/posts/cloudflare-rails-asset-caching/ |
This is not really a bug with Spree, more of an issue with image caching and using a CDN for images served through Rails 5.2 Active Storage, but I figured it might be useful to have a documented solution on this issue for other Spree users.
Issue
Active Storage is simple to set up and get working, but offers no out of the box solution for serving your public facing images through a content delivery network or browser caching, with images having a signed link that expires after 5 minutes, this is great for secure files and download paid for content links, but not ideal for real world usage when serving website product images.
Solution
This solution uses CloudFlare for the CDN and caching.
Work Around
With Spree using active_storage/representations to display images you can simply monkey patch the following file in Rails 5.2.1:
rails-5.2.1/activestorage/app/controllers/active_storage/representations_controller.rb
With the following code
This will then give you Active Storage images served with a public accessible file with a 5 year expire date, if you are running your new Spree app through CloudFlare you can now change your active_storage/* page rule from bypass cache to:
And that should do it, your files will be served through the CloudFlare CDN with browser caching.
Please be aware that this fix may be outdated as Rails updates past version 5.2.1
The text was updated successfully, but these errors were encountered: