Skip to content

A platform for community discussion. Free, open, simple.

replace subfolder on cdn url conversion between general cdn and s3 (#7764)

Jeff Wong893b50031d65
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index cb1e0a1d2b2..dfb1d5e88a8 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
# frozen_string_literal: true
require 'current_user'
require 'canonical_url'
@@ -62,7 +63,8 @@ module ApplicationHelper
if GlobalSetting.use_s3? && GlobalSetting.s3_cdn_url
if GlobalSetting.cdn_url
- path = path.gsub(GlobalSetting.cdn_url, GlobalSetting.s3_cdn_url)
+ folder = ActionController::Base.config.relative_url_root || "/"
+ path = path.gsub(File.join(GlobalSetting.cdn_url, folder, "/"), File.join(GlobalSetting.s3_cdn_url, "/"))
else
# we must remove the subfolder path here, assets are uploaded to s3
# without it getting involved
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index d55023f3e1b..25e75430da9 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
# frozen_string_literal: true
require 'rails_helper'
@@ -32,6 +33,13 @@ describe ApplicationHelper do
expect(helper.preload_script("application")).to include('https://s3cdn.com/assets/application.js')
end
+ it "replaces cdn URLs with s3 cdn subfolder paths" do
+ global_setting :s3_cdn_url, 'https://s3cdn.com/s3_subpath'
+ set_cdn_url "https://awesome.com"
+ ActionController::Base.config.relative_url_root = "/community"
+ expect(helper.preload_script("application")).to include('https://s3cdn.com/s3_subpath/assets/application.js')
+ end
+
it "returns magic brotli mangling for brotli requests" do
helper.request.env["HTTP_ACCEPT_ENCODING"] = 'br'