Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace dependency on canonical-rails with tag helper #386

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
end

gem 'responders'
gem 'canonical-rails'
gem 'solidus_support'
gem 'truncate_html'
gem 'view_component', '~> 3.0'
Expand Down Expand Up @@ -105,7 +104,6 @@
directory 'public', 'public'

copy_file 'config/initializers/solidus_auth_devise_unauthorized_redirect.rb'
copy_file 'config/initializers/canonical_rails.rb'
copy_file 'config/routes/storefront.rb'
copy_file 'config/tailwind.config.js'
create_file 'app/assets/builds/tailwind.css'
Expand Down
36 changes: 36 additions & 0 deletions templates/app/helpers/layout_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module LayoutHelper
# Generates a simple canonical tag based on the request path, preserving allowed
# parameters. For collection actions, a trailing slash is added to the href.
# For more advanced use cases, consider using the `canonical-rails` gem.
#
# @see https://github.com/jumph4x/canonical-rails
# @see https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls
#
# @param host [String] the host to use in the canonical URL
# @param collection_actions [Array<String>] the actions that will include a trailing slash
# @param allowed_parameters [Array<Symbol>] the parameters to preserve in the canonical URL
# @return [String] the generated link rel="canonical" tag
def simple_canonical_tag(
host: current_store&.url,
collection_actions: %w[index],
allowed_parameters: [:keywords, :page, :search, :taxon]
)
path_without_extension = request.path
.sub(/\.#{params[:format]}$/, "")
.sub(/\/$/, "")

href = "#{request.protocol}#{host}#{path_without_extension}"

trailing_slash = request.params.key?('action') &&
collection_actions.include?(request.params['action'])
href += '/' if trailing_slash

query_params = params.select do |key, value|
value.present? && allowed_parameters.include?(key.to_sym)
end.to_unsafe_h

href += "?#{query_params.to_query}" if query_params.present?

tag(:link, rel: :canonical, href: href)
end
end
2 changes: 1 addition & 1 deletion templates/app/views/layouts/storefront.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title><%= title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%== meta_data_tags %>
<%= canonical_tag(current_store.url) %>
<%= simple_canonical_tag %>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>
Expand Down
16 changes: 0 additions & 16 deletions templates/config/initializers/canonical_rails.rb

This file was deleted.

70 changes: 68 additions & 2 deletions templates/spec/system/template_rendering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,80 @@
Capybara.ignore_hidden_elements = true
end

let!(:taxon) {
taxonomy = create :taxonomy, name: "Solidus Brand"
create :taxon, name: "Accessories", taxonomy: taxonomy, parent: taxonomy.root
}

before do
Capybara.ignore_hidden_elements = false

Spree::Store.create!(
code: 'spree',
name: 'My Spree Store',
url: 'spreestore.example.com',
mail_from_address: '[email protected]',
default: true
)
end

it 'layout should have canonical tag referencing site url' do
Spree::Store.create!(code: 'spree', name: 'My Spree Store', url: 'spreestore.example.com', mail_from_address: '[email protected]')

visit root_path

expect(find('link[rel=canonical]')[:href]).to eql('http://spreestore.example.com/')
end

it "renders a canonical tag for products index page with keywords query string" do
create(:product_in_stock, name: 'Solidus Mug', price: 10.00)

visit products_path(keywords: 'solidus')

expect(page).to have_content('Solidus Mug')

expect(
find('link[rel=canonical]')[:href]
).to eql('http://spreestore.example.com/products/?keywords=solidus')
end

it "renders a canonical tag for the products index with a taxon query string" do
create(:product_in_stock, name: 'Solidus Mug', taxons: [taxon])

visit products_path(keywords: 'solidus', taxon: taxon.id)

expect(page).to have_content('Solidus Mug')

expect(
find('link[rel=canonical]')[:href]
).to eql("http://spreestore.example.com/products/?keywords=solidus&taxon=#{taxon.id}")
end

it "renders a canonical tag for taxon pages with a search filter query string" do
create(:product_in_stock, name: 'Solidus Mug', taxons: [taxon])

visit nested_taxons_path(taxon)

within "#sidebar_products_search" do
check "Under $10.00"
click_on "Search"
end
expect(page).to have_content "No products found"

expect(
URI::decode_uri_component find('link[rel=canonical]')[:href]
).to eql(
"http://spreestore.example.com/t/solidus-brand/accessories?search[price_range_any][]=Under+$10.00"
)
end

it "renders a canonical tag for taxon pages with multiple pages of search results" do
create(:product_in_stock, name: 'Solidus Mug 1', taxons: [taxon])
create(:product_in_stock, name: 'Solidus Mug 2', taxons: [taxon])

visit nested_taxons_path(taxon, per_page: 1, page: 2)


expect(
find('link[rel=canonical]')[:href]
).to eql("http://spreestore.example.com/t/solidus-brand/accessories?page=2")
end
end