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
Added a noindex directive to the dashboard, seller panel and API (#14966)
Damian Legawiec88411f1d4e23
diff --git a/.changeset/dashboard-shells-noindex.md b/.changeset/dashboard-shells-noindex.mdnew file mode 100644index 0000000000..229cd52d2f--- /dev/null+++ b/.changeset/dashboard-shells-noindex.md@@ -0,0 +1,5 @@+---+"@spree/cli": patch+---++The dashboard and seller panel starters now tell search engines not to index them: a `<meta name="robots" content="noindex, nofollow">` in each shell, for a panel served from a static host, alongside the `X-Robots-Tag` header the Rails engine adds when it serves a panel itself.diff --git a/packages/dashboard-starter/index.html b/packages/dashboard-starter/index.htmlindex efd6d3785a..fcc3dbf3c2 100644--- a/packages/dashboard-starter/index.html+++ b/packages/dashboard-starter/index.html@@ -3,6 +3,7 @@<head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <meta name="robots" content="noindex, nofollow" /><link rel="icon" type="image/svg+xml" href="/favicon.svg" /><link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" /><link rel="apple-touch-icon" href="/apple-touch-icon.png" />diff --git a/packages/dashboard/index.html b/packages/dashboard/index.htmlindex efd6d3785a..fcc3dbf3c2 100644--- a/packages/dashboard/index.html+++ b/packages/dashboard/index.html@@ -3,6 +3,7 @@<head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <meta name="robots" content="noindex, nofollow" /><link rel="icon" type="image/svg+xml" href="/favicon.svg" /><link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" /><link rel="apple-touch-icon" href="/apple-touch-icon.png" />diff --git a/packages/seller-dashboard-starter/index.html b/packages/seller-dashboard-starter/index.htmlindex 8a696cd4dc..c5bdc72cdf 100644--- a/packages/seller-dashboard-starter/index.html+++ b/packages/seller-dashboard-starter/index.html@@ -3,6 +3,7 @@<head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <meta name="robots" content="noindex, nofollow" /><title>Spree Seller Panel</title></head><body>diff --git a/spree/api/lib/spree/api/engine.rb b/spree/api/lib/spree/api/engine.rbindex 7c012b60b6..c428828e45 100644--- a/spree/api/lib/spree/api/engine.rb+++ b/spree/api/lib/spree/api/engine.rb@@ -49,6 +49,12 @@ module Spreeapp.middleware.insert_before Rack::Runtime, Spree::Api::Middleware::RequestSizeLimitend+ # Outside ShowExceptions, so an unknown API path's 404 carries it too.+ initializer 'spree.api.no_index' do |app|+ require_relative 'middleware/no_index'+ app.middleware.insert_before Rack::Runtime, Spree::Api::Middleware::NoIndex+ end+# The resources agents may read and write are derived from the admin# controllers themselves (see Spree::Api::AgentResourceMap). Marked# stale on every reload rather than rebuilt: deriving it eager-loads thediff --git a/spree/api/lib/spree/api/middleware/no_index.rb b/spree/api/lib/spree/api/middleware/no_index.rbnew file mode 100644index 0000000000..09ac6a137c--- /dev/null+++ b/spree/api/lib/spree/api/middleware/no_index.rb@@ -0,0 +1,33 @@+module Spree+ module Api+ module Middleware+ # Marks every API response as not for search engines. Store API JSON is+ # public and gets indexed once a crawler finds a link to it, where it+ # competes with the storefront's own pages; tokened URLs such as digital+ # downloads travel in emails, which is exactly how crawlers find them.+ #+ # A middleware rather than a controller callback: it also covers the+ # OAuth and webhook controllers that do not inherit from the API base+ # controller, and the 404 Rails renders for an unknown API path. Scoped+ # by path because the same application may serve pages that must stay+ # indexable.+ class NoIndex+ def initialize(app)+ @app = app+ end++ def call(env)+ status, headers, body = @app.call(env)+ headers['x-robots-tag'] = 'noindex, nofollow' if api_request?(env)+ [status, headers, body]+ end++ private++ def api_request?(env)+ env['PATH_INFO']&.start_with?('/api/')+ end+ end+ end+ end+enddiff --git a/spree/api/spec/lib/spree/api/middleware/no_index_spec.rb b/spree/api/spec/lib/spree/api/middleware/no_index_spec.rbnew file mode 100644index 0000000000..32e9e6c7ce--- /dev/null+++ b/spree/api/spec/lib/spree/api/middleware/no_index_spec.rb@@ -0,0 +1,29 @@+require 'spec_helper'++RSpec.describe Spree::Api::Middleware::NoIndex do+ let(:status) { 200 }+ let(:app) { ->(_env) { [status, { 'content-type' => 'application/json' }, ['{}']] } }+ let(:middleware) { described_class.new(app) }++ it 'tells search engines not to index an API response' do+ _status, headers, _body = middleware.call('PATH_INFO' => '/api/v3/store/products')++ expect(headers['x-robots-tag']).to eq('noindex, nofollow')+ end++ context 'when the response is an error' do+ let(:status) { 404 }++ it 'marks it all the same' do+ _status, headers, _body = middleware.call('PATH_INFO' => '/api/v3/nothing-here')++ expect(headers['x-robots-tag']).to eq('noindex, nofollow')+ end+ end++ it 'leaves responses outside the API alone' do+ _status, headers, _body = middleware.call('PATH_INFO' => '/products/shirt')++ expect(headers).not_to have_key('x-robots-tag')+ end+enddiff --git a/spree/api/spec/requests/spree/api/v3/no_index_spec.rb b/spree/api/spec/requests/spree/api/v3/no_index_spec.rbnew file mode 100644index 0000000000..a3cab84845--- /dev/null+++ b/spree/api/spec/requests/spree/api/v3/no_index_spec.rb@@ -0,0 +1,28 @@+require 'spec_helper'++# No API response belongs in a search index: Store API JSON would compete+# with the storefront's pages, and tokened URLs travel in emails.+RSpec.describe 'API responses and search engines', type: :request do+ include_context 'API v3 Store'++ it 'marks a public Store API response noindex' do+ get '/api/v3/store/products', headers: api_key_headers++ expect(response).to have_http_status(:ok)+ expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')+ end++ it 'marks a refused Admin API response noindex' do+ get '/api/v3/admin/products'++ expect(response).to have_http_status(:unauthorized)+ expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')+ end++ it 'marks the 404 for an unknown API path noindex' do+ get '/api/v3/nothing-here'++ expect(response).to have_http_status(:not_found)+ expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')+ end+enddiff --git a/spree/dashboard/app/controllers/spree/dashboard/spa_controller.rb b/spree/dashboard/app/controllers/spree/dashboard/spa_controller.rbindex f7d6c5c9db..42cb1e0b09 100644--- a/spree/dashboard/app/controllers/spree/dashboard/spa_controller.rb+++ b/spree/dashboard/app/controllers/spree/dashboard/spa_controller.rb@@ -24,6 +24,12 @@ module Spree# <script> tags must load, which is exactly that request shape.skip_after_action :verify_same_origin_request+ # A back-office panel has no business in a search index. The header+ # covers every response this controller sends, shell and assets alike,+ # whatever the shell's own markup says; the shells carry a matching+ # <meta name="robots"> for deployments that serve them from elsewhere.+ before_action :mark_not_indexable+def showroot = dist_rootreturn head :not_found unless root@@ -41,6 +47,10 @@ module Spreeprivate+ def mark_not_indexable+ response.headers['X-Robots-Tag'] = 'noindex, nofollow'+ end+# @return [String, nil] directory holding the built bundledef dist_pathraise NotImplementedError, "#{self.class} must implement #dist_path"diff --git a/spree/dashboard/spec/requests/spree/dashboard/app_spec.rb b/spree/dashboard/spec/requests/spree/dashboard/app_spec.rbindex fe19631d89..1ebfd4f027 100644--- a/spree/dashboard/spec/requests/spree/dashboard/app_spec.rb+++ b/spree/dashboard/spec/requests/spree/dashboard/app_spec.rb@@ -58,6 +58,14 @@ RSpec.describe 'Hosted React Dashboard', type: :request doexpect(response.headers['Cache-Control']).to eq('no-cache')end+ it 'tells search engines not to index the shell or its assets' do+ get '/dashboard'+ expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')++ get '/dashboard/assets/app-abc123.js'+ expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')+ end+it 'does not serve files outside the dist directory' dosecret = File.expand_path(File.join(dist, '..', "spree-secret-#{Process.pid}.txt"))File.write(secret, 'top secret')diff --git a/spree/dashboard/spec/requests/spree/seller_panel/app_spec.rb b/spree/dashboard/spec/requests/spree/seller_panel/app_spec.rbindex 3bfa85af30..f719b94066 100644--- a/spree/dashboard/spec/requests/spree/seller_panel/app_spec.rb+++ b/spree/dashboard/spec/requests/spree/seller_panel/app_spec.rb@@ -44,6 +44,14 @@ RSpec.describe 'Hosted seller panel', type: :request doexpect(response.body).to include('seller panel shell')end+ it 'tells search engines not to index the shell or its assets' do+ get '/sellers'+ expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')++ get '/sellers/assets/panel-abc123.js'+ expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')+ end+it 'serves hashed assets as immutable' doget '/sellers/assets/panel-abc123.js'