Skip to content
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

Delivering assets through CloudFlare requires monkey-patch #44136

Closed
sedubois opened this issue Jan 10, 2022 · 3 comments · Fixed by #48869
Closed

Delivering assets through CloudFlare requires monkey-patch #44136

sedubois opened this issue Jan 10, 2022 · 3 comments · Fixed by #48869

Comments

@sedubois
Copy link
Contributor

sedubois commented Jan 10, 2022

Steps to reproduce

It appears that delivering ActiveStorage assets through CloudFlare (free plan) can be done as suggested here + here:

  • add Rails.application.config.active_storage.resolve_model_to_route = :rails_storage_proxy to config/initializers/active_storage.rb OR use rails_storage_proxy_path in the views (e.g. rails_storage_proxy_path(content.poster.variant(resize_to_limit: [size, size]))). There is it seems no need to define a direct :cdn_image like in the docs, as the CDN host remains the same when using CloudFlare.
  • monkey-patch request.session_options[:skip] = true in the ProxyControllers (ref)

Expected behavior

  • No monkey-patch should be needed.
  • It would be helpful to have some documentation to use CloudFlare with ActiveStorage.

Actual behavior

After doing the above, I can see my images have headers cache-control: public, max-age=3155695200 and cf-cache-status: HIT (after the second request). However, a monkey-patch was necessary to achieve this, so @zzak recommended I open this issue.

System configuration

Rails version: 7.0.0

Ruby version: 3.0.3

@rafaelfranca
Copy link
Member

This is mostly a feature request, so I think you either should implement it or start a thread on Rails Core discussion board to see if anyone is interested in implement it.

@lypanov
Copy link

lypanov commented Jul 26, 2022

How rude.

@brenogazzola
Copy link
Contributor

brenogazzola commented Jul 26, 2022

An alternative to monkey patching is to use one of the limited page rules that comes with free plans to force cloudflare to cache:

URL: https://example.com/rails/active_storage/representations/proxy*
Browser Cache TTL: a year
Cache Level: Cache everything
Edge Cache TTL: a month

paulreece pushed a commit to paulreece/rails-paulreece that referenced this issue Aug 26, 2023
…roller

This allows CDNs such as CloudFlare to cache files delivered by proxy by default

Fix rails#44136
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants