If you use ActiveStorage and you have a page with N images you get N additional requests to your Rails app (i.e. N redirects). That means wasting a lot of server resources if yo
After days of reasoning and tests, I am really excited of my final solution, which I explain below. This is an opinionated approach to images and may not represent the current Rails Way™️, however it has incredible advantages for websites that serve many public images, in particular:
Here's the solution:
storage.example.com
) - you can even disable the public access at bucket level and allow access only to the Cloudflare ips using a bucket policystorage.example.com
that points to your domain; you need to use Flexible SSL (you can use a page rule for the subdomain); use page rules to set heavy caching: set Cache Everything and set a very long value (e.g. 1 year) for Browser Cache TTL and Edge Cache TTL@post.variant(...).processed
after every update or creation of @post
; then in your views use 'https://storage.example.com/' + @post.variant(...).key'
(note that we don't call processed
here in the views to avoid additional checks in s3); you can also have a rake task that calls processed
on each object, in case you need to regenerate the variants; this is works perfectly if you have only a few variants (e.g. 1 image / variant per post) that are changed infrequentlyMost of the above steps are optional, so you can combine them based on your needs.
You can use the service_url to create direct links to your resources.
We don't use Rails views in our project so my knowledge about the view layer is rusty. I think you could put it in a dedicated helper and then use it from your views.