Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Set cdn_image_url correctly (#14273)
Kacper Mękarski7958a81bf8cb
diff --git a/spree/core/config/routes.rb b/spree/core/config/routes.rbindex 8d9f9374d8..65853fecef 100644--- a/spree/core/config/routes.rb+++ b/spree/core/config/routes.rb@@ -2,11 +2,11 @@# see: https://github.com/rails/rails/issues/34872Rails.application.routes.draw dodirect :cdn_image do |model, options|- opts = options.slice(:protocol, :host, :port)- opts[:host] = Spree.cdn_host if Spree.cdn_host.present?- opts[:host] ||= Rails.application.routes.default_url_options[:host]- opts[:host] ||= Spree::Store.current.url_or_custom_domain if Spree::Store.current.present?-+ opts = options.slice(:protocol, :port)+ opts[:host] = Spree.cdn_host.presence ||+ Rails.application.routes.default_url_options[:host].presence ||+ options[:host].presence ||+ (Spree::Store.current.present? ? Spree::Store.current.formatted_url : nil)opts[:only_path] = true if opts[:host].blank?if model.blob.service_name == 'cloudinary' && defined?(Cloudinary)diff --git a/spree/emails/spec/mailers/spree/mailer_image_host_spec.rb b/spree/emails/spec/mailers/spree/mailer_image_host_spec.rbnew file mode 100644index 0000000000..f9e5c3e0cb--- /dev/null+++ b/spree/emails/spec/mailers/spree/mailer_image_host_spec.rb@@ -0,0 +1,62 @@+require 'spec_helper'++describe 'image URLs in emails', type: :mailer do+ let(:backend_host) { 'backend.example.com' }+ let(:storefront_origin) { 'https://storefront.example.com' }++ let(:store) { create(:store, url: 'store.example.com') }+ let!(:allowed_origin) { create(:allowed_origin, store: store, origin: storefront_origin) }++ let(:order) { create(:completed_order_with_totals, store: store, email: '[email protected]') }++ let(:message) { Spree::OrderMailer.confirm_email(order) }+ let(:html_body) { (message.html_part || message).body.to_s }+ let(:image_srcs) { html_body.scan(/<img[^>]+src="([^"]+)"/).flatten }+ let(:active_storage_srcs) { image_srcs.select { |src| src.include?('/rails/active_storage/') } }++ around do |example|+ original_routes_host = Rails.application.routes.default_url_options[:host]+ original_mailer_host = ActionMailer::Base.default_url_options[:host]++ example.run++ Rails.application.routes.default_url_options[:host] = original_routes_host+ ActionMailer::Base.default_url_options[:host] = original_mailer_host+ end++ before do+ store.logo.attach(+ io: File.new(Spree::Core::Engine.root + 'spec/fixtures' + 'thinking-cat.jpg'),+ filename: 'thinking-cat.jpg'+ )++ # Production setup+ Rails.application.routes.default_url_options[:host] = backend_host+ end++ it 'sets the storefront allowed origin as the email link host' do+ message.deliver_now++ expect(ActionMailer::Base.default_url_options[:host]).to eq(storefront_origin)+ end++ it 'generates image urls against the backend application host, not the storefront origin' do+ expect(active_storage_srcs).not_to be_empty+ active_storage_srcs.each do |src|+ expect(src).to include(backend_host)+ expect(src).not_to include('storefront.example.com')+ end+ end++ context 'when Spree.cdn_host is configured' do+ before { allow(Spree).to receive(:cdn_host).and_return('cdn.example.com') }++ it 'prefers the cdn host over both the backend and storefront hosts' do+ active_storage_srcs.each do |src|+ expect(src).to include('cdn.example.com')+ expect(src).not_to include(backend_host)+ expect(src).not_to include('storefront.example.com')+ end+ end+ end+end