Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Remove canonical-rails as a dependency (#13419)

Damian Legawiecd89fa0037196
diff --git a/storefront/Gemfile b/storefront/Gemfile
index bd0afdf5b5..3303bc17ce 100644
--- a/storefront/Gemfile
+++ b/storefront/Gemfile
@@ -7,7 +7,6 @@ gem 'spree_emails', path: '../emails'
gem 'spree_page_builder', path: '../packages/page_builder'
gem 'devise'
-gem 'canonical-rails', github: 'commonlit/canonical-rails', branch: "bump-rails-8-1" # temporary fix for Rails 8.1 compatibility
gem 'sprockets-rails', '>= 3.5.2'
group :test, :development do
diff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rb
index e0f5529018..987d3b4eb7 100644
--- a/storefront/app/controllers/spree/store_controller.rb
+++ b/storefront/app/controllers/spree/store_controller.rb
@@ -15,6 +15,7 @@ module Spree
helper 'spree/base'
helper 'spree/locale'
helper 'spree/storefront_locale'
+ helper 'spree/canonical_url'
helper 'spree/currency'
helper 'spree/addresses'
helper 'spree/wishlist'
diff --git a/storefront/app/helpers/spree/canonical_url_helper.rb b/storefront/app/helpers/spree/canonical_url_helper.rb
new file mode 100644
index 0000000000..b83b629541
--- /dev/null
+++ b/storefront/app/helpers/spree/canonical_url_helper.rb
@@ -0,0 +1,27 @@
+module Spree
+ module CanonicalUrlHelper
+ # Generates a canonical link tag for the current page
+ #
+ # @param host [String] The host/domain to use (defaults to current store's domain)
+ # @return [ActiveSupport::SafeBuffer] The canonical link tag HTML
+ def canonical_tag(host = nil)
+ tag.link(href: canonical_href(host), rel: :canonical)
+ end
+
+ # Returns the full canonical URL for the current request
+ #
+ # @param host [String] The host/domain to use (defaults to request host)
+ # @return [String] The full canonical URL
+ def canonical_href(host = nil)
+ host ||= request.host
+ "#{request.protocol}#{host}#{canonical_path}"
+ end
+
+ # Returns the canonical path for the current request (without query parameters)
+ #
+ # @return [String] The canonical path
+ def canonical_path
+ request.path.presence || '/'
+ end
+ end
+end
diff --git a/storefront/lib/spree/storefront.rb b/storefront/lib/spree/storefront.rb
index c013236c96..d50637e16f 100644
--- a/storefront/lib/spree/storefront.rb
+++ b/storefront/lib/spree/storefront.rb
@@ -2,7 +2,6 @@ require 'spree_core'
require 'spree_page_builder'
require 'active_link_to'
-require 'canonical-rails'
require 'heroicon'
require 'importmap-rails'
require 'local_time'
diff --git a/storefront/spec/helpers/spree/canonical_url_helper_spec.rb b/storefront/spec/helpers/spree/canonical_url_helper_spec.rb
new file mode 100644
index 0000000000..b2fdacb23a
--- /dev/null
+++ b/storefront/spec/helpers/spree/canonical_url_helper_spec.rb
@@ -0,0 +1,58 @@
+require 'spec_helper'
+
+RSpec.describe Spree::CanonicalUrlHelper, type: :helper do
+ let(:request_double) do
+ double('request',
+ host: 'example.com',
+ protocol: 'https://',
+ path: '/products/test-product'
+ )
+ end
+
+ before do
+ allow(helper).to receive(:request).and_return(request_double)
+ end
+
+ describe '#canonical_path' do
+ it 'returns the request path' do
+ expect(helper.canonical_path).to eq('/products/test-product')
+ end
+
+ context 'with blank path' do
+ before do
+ allow(request_double).to receive(:path).and_return('')
+ end
+
+ it 'returns root path' do
+ expect(helper.canonical_path).to eq('/')
+ end
+ end
+ end
+
+ describe '#canonical_href' do
+ it 'returns full URL with protocol, host, and path' do
+ expect(helper.canonical_href).to eq('https://example.com/products/test-product')
+ end
+
+ context 'with custom host' do
+ it 'uses the provided host' do
+ expect(helper.canonical_href('store.example.com')).to eq('https://store.example.com/products/test-product')
+ end
+ end
+ end
+
+ describe '#canonical_tag' do
+ it 'returns a link tag with canonical rel' do
+ result = helper.canonical_tag
+ expect(result).to include('rel="canonical"')
+ expect(result).to include('href="https://example.com/products/test-product"')
+ end
+
+ context 'with custom host' do
+ it 'uses the provided host in the href' do
+ result = helper.canonical_tag('store.example.com')
+ expect(result).to include('href="https://store.example.com/products/test-product"')
+ end
+ end
+ end
+end
diff --git a/storefront/spree_storefront.gemspec b/storefront/spree_storefront.gemspec
index 1db9e8e52b..84240ce664 100644
--- a/storefront/spree_storefront.gemspec
+++ b/storefront/spree_storefront.gemspec
@@ -27,7 +27,6 @@ Gem::Specification.new do |s|
s.add_dependency 'spree_page_builder', ">= #{s.version}"
s.add_dependency 'active_link_to'
- s.add_dependency 'canonical-rails', '~> 0.2.14'
s.add_dependency 'heroicon'
s.add_dependency 'importmap-rails'
s.add_dependency 'inline_svg', '~> 1.10'
diff --git a/template.rb b/template.rb
index 32d38dedb6..87fe1322f9 100644
--- a/template.rb
+++ b/template.rb
@@ -15,8 +15,6 @@ def add_gems
# Core dependencies
gem 'devise'
- gem 'canonical-rails', github: 'commonlit/canonical-rails', branch: "bump-rails-8-1" # temporary fix for Rails 8.1 compatibility
-
# Spree gems - using main branch for latest
gem 'spree', USE_LOCAL_SPREE ? { path: '../' } : { version: SPREE_VERSION }
gem 'spree_emails', USE_LOCAL_SPREE ? { path: '../' } : { version: SPREE_VERSION }