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.

Add missing SEO routes (#12686)

Damian Kaczmarczyk4080e8b30f09
diff --git a/.circleci/config.yml b/.circleci/config.yml
index cb0bf59c20..e12f442c3c 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -146,7 +146,7 @@ jobs:
environment:
<<: *mysql_environment
PROJECTS: emails,sample
-
+
workflows:
main:
jobs:
diff --git a/storefront/app/views/spree/seo/robots.text.erb b/storefront/app/views/spree/seo/robots.text.erb
index 6dfd31bd5a..ce87a5d94b 100644
--- a/storefront/app/views/spree/seo/robots.text.erb
+++ b/storefront/app/views/spree/seo/robots.text.erb
@@ -27,5 +27,5 @@ Disallow: /shipment_trackings
Disallow: /locale
Disallow: /currency
-Sitemap: <%= sitemap_url(host: store.formatted_url_or_custom_domain, format: :xml) %>
+Sitemap: <%= spree.sitemap_url(host: store.formatted_url_or_custom_domain, format: :xml) %>
<% end %>
diff --git a/storefront/config/routes.rb b/storefront/config/routes.rb
index 1832370ff2..d55763282b 100644
--- a/storefront/config/routes.rb
+++ b/storefront/config/routes.rb
@@ -85,4 +85,8 @@ Spree::Core::Engine.add_routes do
root to: 'home#index'
end
+
+ get 'robots.txt' => 'seo#robots'
+ get 'sitemap' => 'seo#sitemap'
+ get 'sitemap.xml.gz' => 'seo#sitemap'
end
diff --git a/storefront/spec/requests/robots_spec.rb b/storefront/spec/requests/robots_spec.rb
new file mode 100644
index 0000000000..e7163041f1
--- /dev/null
+++ b/storefront/spec/requests/robots_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe 'Robots.txt', type: :request do
+ let(:store) { @default_store }
+
+ context 'when the store does not prefer to be indexed in search engines' do
+ it 'returns the robots.txt file' do
+ get '/robots.txt'
+
+ expect(response).to have_http_status(:success)
+ expect(response.body).to include('User-agent: *')
+ expect(response.body).to include('Disallow: /')
+ end
+ end
+
+ context 'when the store prefers to be indexed in search engines' do
+ around do |example|
+ preferred_index_in_search_engines = store.preferred_index_in_search_engines
+ store.update(preferred_index_in_search_engines: true)
+ example.run
+ store.update(preferred_index_in_search_engines: preferred_index_in_search_engines)
+ end
+
+ it 'returns the robots.txt file' do
+ get '/robots.txt'
+
+ expect(response).to have_http_status(:success)
+ expect(response.body).to include('User-agent: *')
+ expect(response.body).not_to include(/^Disallow: \/$/)
+ expect(response.body).to include("Sitemap: #{spree.sitemap_url(host: store.formatted_url_or_custom_domain, format: :xml, port: nil)}")
+ end
+ end
+end
diff --git a/storefront/spec/requests/sitemap_spec.rb b/storefront/spec/requests/sitemap_spec.rb
new file mode 100644
index 0000000000..137271b94a
--- /dev/null
+++ b/storefront/spec/requests/sitemap_spec.rb
@@ -0,0 +1,81 @@
+require 'spec_helper'
+
+describe 'Sitemap', type: :request do
+ let(:store) { @default_store }
+
+ context 'search engines disabled' do
+ around do |example|
+ preferred_index_in_search_engines = store.preferred_index_in_search_engines
+ store.update(preferred_index_in_search_engines: false)
+ example.run
+ store.update(preferred_index_in_search_engines: preferred_index_in_search_engines)
+ end
+
+ context 'when requesting gzipped sitemap' do
+ it 'returns 404' do
+ get '/sitemap.xml.gz'
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context 'when requesting xml sitemap' do
+ it 'returns 404' do
+ get '/sitemap.xml'
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+ end
+
+ context 'search engines enabled' do
+ around do |example|
+ preferred_index_in_search_engines = store.preferred_index_in_search_engines
+ store.update(preferred_index_in_search_engines: true)
+ example.run
+ store.update(preferred_index_in_search_engines: preferred_index_in_search_engines)
+ end
+
+ it 'returns sitemap' do
+ get '/sitemap.xml'
+ expect(response).to have_http_status(:success)
+ expect(response.content_type).to eq('application/xml; charset=utf-8')
+ expect(response.body).to start_with('<?xml version="1.0" encoding="UTF-8"?>')
+ expect(response.body).to include('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
+ end
+
+ it 'also works for xml.gz format' do
+ get '/sitemap.xml.gz'
+ expect(response).to have_http_status(:success)
+ expect(response.content_type).to eq('application/x-gzip')
+ expect(response.headers['Content-Disposition']).to include('inline')
+ expect(response.headers['Content-Disposition']).to include('filename="sitemap.xml.gz"')
+
+ # Decompress and check content
+ gz = Zlib::GzipReader.new(StringIO.new(response.body))
+ xml_content = gz.read
+ expect(xml_content).to start_with('<?xml version="1.0" encoding="UTF-8"?>')
+ expect(xml_content).to include('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
+ end
+
+ context 'with content' do
+ let!(:products) { create_list(:product, 5, stores: [store], status: :active) }
+ let!(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:taxons) { create_list(:taxon, 5, taxonomy: taxonomy) }
+
+ it 'containts links to products and taxons' do
+ get '/sitemap.xml'
+ expect(response).to have_http_status(:success)
+
+ xml = Nokogiri::XML(response.body)
+ urls = xml.xpath('//xmlns:url/xmlns:loc').map(&:text)
+
+ products.each do |product|
+ expect(urls).to include(spree.product_url(product, host: store.url, port: Capybara.server_port))
+ end
+
+ taxonomy.taxons.each do |taxon|
+ expect(urls).to include(spree.nested_taxons_url(taxon, host: store.url, port: Capybara.server_port))
+ end
+ end
+ end
+ end
+end
diff --git a/storefront/spec/spec_helper.rb b/storefront/spec/spec_helper.rb
index 3a225b1c01..0dc7580ce6 100644
--- a/storefront/spec/spec_helper.rb
+++ b/storefront/spec/spec_helper.rb
@@ -97,6 +97,10 @@ RSpec.configure do |config|
config.order = :random
Kernel.srand config.seed
+ Capybara.app_host = 'http://app.' + Spree.root_domain
+ Capybara.server_port = 3000
+ Capybara.test_id = 'data-test-id'
+
config.filter_run_including focus: true unless ENV['CI']
config.run_all_when_everything_filtered = true
end