cdn-image-tag 0.0 → 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fadd836c3c3e48ed3b05fc0681f49aac84d460b10236254a55ce6edd12cbd3e8
4
- data.tar.gz: 2f6e8707ace19287b43af52547672883f7fdd0a0fba2a394d08d9c0e9c0a4f9c
3
+ metadata.gz: 7b309b40b05d9d8d915847f2c102c2b5e0b459a525b6b978f81ee70d060dca90
4
+ data.tar.gz: 1a4e999e6aef4729065b1079e4c62b30c48f162f63b262729163b95d4d88a389
5
5
  SHA512:
6
- metadata.gz: 16210151ef66fe0ab21049cec8b94f5f5753121e206aa98e97c455191b89a0c1acb3fa83e8c1f0e3cc120ec56353f2e91ac26f1af9d82081b630e0f16a93c870
7
- data.tar.gz: 77692cc2829e3008c4ffe4fa181709b4be92e36298c3c3e4f64080b41c5b69bcc92eec15a1d3dabc149860df94f58bc45c6e2a0385eea2007a50607b2ddd83c6
6
+ metadata.gz: 68aa9789622898cca1e451d9d7681d34c3bfcb3c75cd4b9eb790751a5cf5a1002f3032d225afb182585a7a5d75c819aa612e22e916f1facb94bc7ea40dbe0c8d
7
+ data.tar.gz: 22b43ed47dcec6d9dea57e4f58aaaa70d25f49980f297778d023272c94f0bff52ad0f5cc33e5d276e0449f2c20b273138a42b3ab025a342f8b565877931631d5
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+
2
+
3
+ This gem is for Rails 8 to allow you to serve images + Active Storage assets from a CDN, via Active Storage proxy.
4
+
5
+
6
+ In Active storage proxy mode, assets are stored on S3 but served from the Rails app.
7
+
8
+ To speed performance and reduce load on the Rails app, you use a CDN that **points to the Rails app itself** (not S3)
9
+
10
+ Go to your CDN and create a distribution with an origin that points back to the domain of your Rails app.
11
+
12
+ # AWS Cloudfront
13
+
14
+ For AWS Cloudfront: On the Origin edit screen, set these:
15
+
16
+ - Protocol: "HTTPS only"
17
+ - Minimum Origin SSL protocol: "TLSv1.2"
18
+
19
+
20
+ Then go to the Behaviors tab on the distribution, select the default behavior ("0"), and check these settings:
21
+
22
+ - Viewer protocol policy: Https only
23
+ - Allowed HTTP methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
24
+ - Restrict viewer access: No
25
+ - Cache policy: CachingOptimized
26
+ - Origin request policy: AllViewerExceptHostHeader
27
+
28
+
29
+ # INSTALL
30
+ - Setup S3 (see Active Storage docs + example below)
31
+ - Set Cloudfront or another CDN (see above)
32
+ - Set an ENV variable `CDN_TO_SELF` to the CDN distribution domain; (sub)domain only (not including protocol)
33
+ - Include this gem in your gemfile
34
+ - Wherever you would use `image_tag`, instead use `cdn_image_tag`
35
+
36
+ // production.rb
37
+
38
+ ```
39
+ config.active_storage.service = :amazon
40
+ config.action_mailer.asset_host = "https://#{ENV['CDN_TO_SELF']}"
41
+ config.active_storage.cdn_host = ENV['CDN_TO_SELF']
42
+ config.active_storage.routes_host = ENV["CDN_TO_SELF"]
43
+ config.active_storage.resolve_model_to_route = :rails_storage_proxy
44
+ ```
45
+ // DO NOT SET config.action_controller.default_url_options
46
+
47
+
48
+ // storage.yml
49
+ ```
50
+ amazon:
51
+ service: S3
52
+ access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
53
+ secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
54
+ region: <%= ENV['AWS_REGION'] %>
55
+ bucket: myapp-<%= Rails.env %>
56
+ ```
57
+
58
+ (I like my bucket names to be `myapp-production` and `myapp-devlepment` to match the Rails env, but you can modify this as needed)
59
+
60
+ Verify:
61
+ - Deploy your app, look at an image, right-click to "Open image in new tab", and confirm that the CDN is used instead of the Rails app domain.
62
+ - You can swap the CDN domain for your Rails app domain and get the same image
63
+ - The images from the asset pipeline should have the pipeline's thumbprint
64
+ - Images served from Active storage have URLs like `/rails/active_storage/representations/proxy/`
@@ -0,0 +1,72 @@
1
+ # app/helpers/cdn_image_helper.rb
2
+ module CdnImageHelper
3
+ #
4
+ # Public method — drop-in replacement for image_tag.
5
+ #
6
+ # Works with:
7
+ # cdn_image_tag("logo.png")
8
+ # cdn_image_tag(@record.image)
9
+ #
10
+ def cdn_image_tag(source, **options)
11
+ image_tag(cdn_image_url(source), **options)
12
+ end
13
+
14
+ #
15
+ # Builds the URL, then rewrites the host if CDN is configured.
16
+ #
17
+ def cdn_image_url(source)
18
+ url = if active_storage?(source)
19
+ url_for(source)
20
+ else
21
+ asset_pipeline_url(source)
22
+ end
23
+
24
+ rewrite_to_cdn(url)
25
+ end
26
+
27
+ private
28
+
29
+ # True if this is an ActiveStorage attachment or representation.
30
+ def active_storage?(source)
31
+ source.respond_to?(:attachment) ||
32
+ source.respond_to?(:blob) ||
33
+ source.respond_to?(:processed) ||
34
+ source.respond_to?(:representable?)
35
+ end
36
+
37
+ #
38
+ # Always get a digested pipeline asset URL.
39
+ #
40
+ # asset_url returns either:
41
+ # - full URL if default_url_options[:host] is set
42
+ # - relative path (/assets/...) otherwise — which is perfect
43
+ #
44
+ def asset_pipeline_url(source)
45
+ ActionController::Base.helpers.asset_url(source)
46
+ end
47
+
48
+ #
49
+ # Rewrites any URL (relative or absolute) to use the CDN hostname.
50
+ #
51
+ def rewrite_to_cdn(url)
52
+ cdn_host = ENV["CDN_TO_SELF"]
53
+ return url if cdn_host.blank? # local dev
54
+
55
+ uri = URI.parse(url)
56
+
57
+ # If it's a relative path, construct a full CDN URL
58
+ if uri.host.nil?
59
+ return URI::HTTPS.build(
60
+ host: cdn_host,
61
+ path: uri.path,
62
+ query: uri.query
63
+ ).to_s
64
+ end
65
+
66
+ # Absolute URL → rewrite scheme + host
67
+ uri.scheme = "https"
68
+ uri.host = cdn_host
69
+ uri.port = nil
70
+ uri.to_s
71
+ end
72
+ end
@@ -0,0 +1,6 @@
1
+ require "cdnimagetag/engine"
2
+
3
+ module CdnImageTag
4
+
5
+
6
+ end
@@ -1,5 +1,5 @@
1
1
  module CdnImageTag
2
2
  class Version
3
- CURRENT = '0.0'
3
+ CURRENT = '0.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdn-image-tag
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.0'
4
+ version: '0.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Fleetwood-Boldt
@@ -30,6 +30,9 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - README.md
34
+ - app/helpers/cdn_image_helper.rb
35
+ - lib/cdn-image-tag.rb
33
36
  - lib/cdnimagetag/engine.rb
34
37
  - lib/cdnimagetag/version.rb
35
38
  homepage: ''