Skip to content

Instantly share code, notes, and snippets.

@timm-oh
timm-oh / application_helper.rb
Last active December 17, 2023 21:04
Rails 5.2.3: Cache Active Storage Blobs and Variants through Cloudfront
# app/helpers/application_helper.rb
module ApplicationHelper
# active_storage_item could be a blob or variant object
def proxy_url(active_storage_item, options = {})
options.merge!(host: ENV['ASSETS_HOST']) if ENV['ASSETS_HOST'].present?
# proxy: 'true' allows you to stil have the original functionality while
# being able to proxy through a CDN. You've got to ensure that your CDN
# forwards this param otherwise active storage will always do the default
# behavior which is a redirect to the service.