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
Getting started - simplify tracking when storefront is connected (#14288)
Damian Legawiec7ee146dae748
diff --git a/CLAUDE.md b/CLAUDE.mdindex 13858cd2d8..82814b8abc 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -103,6 +103,7 @@ Backend: http://localhost:3000, admin at `/admin` (`[email protected]` / `spree1- Commit message body: be precise, DON'T include implementation detail, focus on the "what" and "why", not the "how"- If n-commits are needed for a single logical change, use `git commit --fixup` for the follow-ups and `git rebase -i --autosquash` to combine into a single commit before merging- Documentation also needs to follow the same principles — focus on the "what" and "why", not the "how". Don't include implementation details in docs. Docs should explain the feature, its purpose, and how to use it, but not how it's implemented internally.+- NEVER commit anything to main branch, always use feature/fix/chore branches for development## Backend (Ruby)diff --git a/spree/admin/app/controllers/spree/admin/storefront_controller.rb b/spree/admin/app/controllers/spree/admin/storefront_controller.rbindex e3a6e0ef40..1f498a4a1d 100644--- a/spree/admin/app/controllers/spree/admin/storefront_controller.rb+++ b/spree/admin/app/controllers/spree/admin/storefront_controller.rb@@ -20,15 +20,15 @@ module Spree# PATCH /admin/storefront## Saves the storefront URL preference (used as the base for links in- # customer emails and as the "connected" signal for non-web storefronts),- # optionally adding it as an allowed origin for browser-based storefronts.+ # customer emails and as the setup-task completion signal) and adds it+ # as an allowed origin so browser-based storefronts can call the Store API.def updateorigin = normalize_origin(params[:storefront_url])if origin.nil?flash[:error] = Spree.t('admin.storefront_setup.invalid_origin')elsif current_store.update(preferred_storefront_url: origin)- current_store.allowed_origins.find_or_create_by(origin: origin) if params[:add_allowed_origin] == '1'+ current_store.allowed_origins.find_or_create_by(origin: origin)flash[:success] = Spree.t('admin.storefront_setup.storefront_url_saved', url: origin)elseflash[:error] = current_store.errors.full_messages.to_sentencediff --git a/spree/admin/app/views/spree/admin/storefront/show.html.erb b/spree/admin/app/views/spree/admin/storefront/show.html.erbindex fdb8d446ee..ea64c16e9d 100644--- a/spree/admin/app/views/spree/admin/storefront/show.html.erb+++ b/spree/admin/app/views/spree/admin/storefront/show.html.erb@@ -76,6 +76,7 @@<% end %><% end %></div>+ <p class="text-gray-500 text-sm mb-3"><%= Spree.t('admin.storefront_setup.deploy_finish_hint') %></p><%= external_link_to Spree.t('admin.storefront_setup.local_installation_instructions'), 'https://spreecommerce.org/docs/developer/storefront/nextjs/quickstart' %></div></div>@@ -120,12 +121,6 @@placeholder="https://myshop.com" value="<%= current_store.preferred_storefront_url %>"><p class="text-gray-500 text-sm mt-1 mb-0"><%= Spree.t('admin.storefront_setup.storefront_url_help') %></p></div>- <div class="form-checkbox">- <%= check_box_tag :add_allowed_origin, '1', true, class: 'custom-control-input' %>- <label class="custom-control-label" for="add_allowed_origin">- <%= Spree.t('admin.storefront_setup.add_allowed_origin_label') %>- </label>- </div><div><button type="submit" class="btn btn-primary"><%= Spree.t('actions.save') %></button></div>diff --git a/spree/admin/config/locales/en.yml b/spree/admin/config/locales/en.ymlindex 1e5309d969..b6448479b6 100644--- a/spree/admin/config/locales/en.yml+++ b/spree/admin/config/locales/en.yml@@ -548,14 +548,14 @@ en:setup: Setup Tax Ratesstorefront: Storefrontstorefront_setup:- add_allowed_origin_label: Also add this URL as an allowed origin — required for browser-based storefronts to call the Store APIallow_origin_button: Allow this origin & finish setupapi_url_label: Store API URLconnect_copy: 'Use these credentials in any client that talks to the Store API — a web storefront, a mobile app, or anything else:'connect_title: Connect your storefrontconnected_title: Connected storefrontsdeploy_button: Deploy to Vercel- deploy_copy: Don't have a storefront yet? The fastest way to launch is the official open-source Next.js storefront. Deploying clones it to your Git account and builds it on Vercel with your credentials prefilled — once it finishes, you'll be redirected back here to complete the setup.+ deploy_copy: Don't have a storefront yet? The fastest way to launch is the official open-source Next.js storefront. Deploying clones it to your Git account and builds it on Vercel with your credentials prefilled.+ deploy_finish_hint: Once the deployment finishes, copy your storefront's URL from Vercel into the Storefront URL field below and save it to finish the setup.deploy_title: 'Recommended: deploy the official Next.js storefront'deployed_copy: It's now live atdeployed_title: Your storefront has been deployed!@@ -570,7 +570,7 @@ en:origin_allowed: "%{origin} can now access your Store API."publishable_key_label: Publishable API keysee_online_demo: See online demo- storefront_url_help: The URL customers use to reach your storefront. It becomes the base for links in customer emails — mobile-only setups can point it at their landing page.+ storefront_url_help: The URL customers use to reach your storefront. It becomes the base for links in customer emails and is automatically added as an allowed origin so browser-based storefronts can call the Store API.storefront_url_label: Storefront URLstorefront_url_saved: 'Storefront URL saved: %{url}'title: Setup Storefrontdiff --git a/spree/admin/spec/controllers/spree/admin/storefront_controller_spec.rb b/spree/admin/spec/controllers/spree/admin/storefront_controller_spec.rbindex 92fd54f1db..9feef7ab63 100644--- a/spree/admin/spec/controllers/spree/admin/storefront_controller_spec.rb+++ b/spree/admin/spec/controllers/spree/admin/storefront_controller_spec.rb@@ -102,7 +102,7 @@ RSpec.describe Spree::Admin::StorefrontController, type: :controller dodescribe 'PATCH #update' dosubject(:update_storefront) { patch :update, params: params }- let(:params) { { storefront_url: 'myshop.com', add_allowed_origin: '1' } }+ let(:params) { { storefront_url: 'myshop.com' } }it 'saves the normalized storefront url and allows the origin' doexpect { update_storefront }.to change { store.allowed_origins.count }.by(1)@@ -113,13 +113,13 @@ RSpec.describe Spree::Admin::StorefrontController, type: :controller doexpect(response).to redirect_to(spree.admin_storefront_path)end- context 'without the add_allowed_origin flag' do- let(:params) { { storefront_url: 'https://app.myshop.com' } }+ context 'when the origin is already allowed' do+ before { create(:allowed_origin, store: store, origin: 'https://myshop.com') }- it 'only saves the preference' do+ it 'does not duplicate it' doexpect { update_storefront }.not_to change(Spree::AllowedOrigin, :count)- expect(store.reload.preferred_storefront_url).to eq('https://app.myshop.com')+ expect(store.reload.preferred_storefront_url).to eq('https://myshop.com')endenddiff --git a/spree/core/app/models/concerns/spree/stores/setup.rb b/spree/core/app/models/concerns/spree/stores/setup.rbindex e61739f8fe..cc5337c2ab 100644--- a/spree/core/app/models/concerns/spree/stores/setup.rb+++ b/spree/core/app/models/concerns/spree/stores/setup.rb@@ -51,26 +51,10 @@ module Spreepayment_methods.active.where.not(type: Spree::PaymentMethod::StoreCredit.to_s).any?end- # A storefront counts as set up once an active publishable key has actually- # authenticated a Store API request and a storefront is connected.+ # A storefront counts as set up once the merchant has saved the storefront+ # URL (entered manually or backfilled by the Vercel callback).def storefront_setup?- storefront_publishable_key_used? && storefront_connected?- end-- def storefront_publishable_key_used?- api_keys.active.publishable.where.not(last_used_at: nil).exists?- end-- # A non-loopback allowed origin (web storefronts) or an explicit storefront- # URL (mobile apps and other clients that don't need CORS) both count as a- # connected storefront; the `http://localhost` origin seeded on install- # doesn't.- def storefront_connected?- storefront_origin_added? || preferred_storefront_url.present?- end-- def storefront_origin_added?- allowed_origins.reject(&:loopback?).any?+ preferred_storefront_url.present?endendenddiff --git a/spree/core/spec/models/spree/store_spec.rb b/spree/core/spec/models/spree/store_spec.rbindex 0fb58c222f..91f9d030fe 100644--- a/spree/core/spec/models/spree/store_spec.rb+++ b/spree/core/spec/models/spree/store_spec.rb@@ -960,10 +960,9 @@ describe Spree::Store, type: :model, without_global_store: true dodescribe '#storefront_setup?' dosubject { store.storefront_setup? }- context 'with a used publishable key and a non-loopback allowed origin' do+ context 'with a saved storefront URL' dobefore do- create(:api_key, store: store, last_used_at: 1.hour.ago)- create(:allowed_origin, store: store, origin: 'https://shop.example.com')+ store.update!(preferred_storefront_url: 'https://shop.example.com')endit { is_expected.to be true }@@ -973,40 +972,14 @@ describe Spree::Store, type: :model, without_global_store: true doendend- context 'when the publishable key has never been used' do+ context 'without a saved storefront URL' dobefore do- create(:api_key, store: store)create(:allowed_origin, store: store, origin: 'https://shop.example.com')end- it { is_expected.to be false }- end-- context 'when the used publishable key is revoked' do- before do- create(:api_key, :revoked, store: store, last_used_at: 1.hour.ago)- create(:allowed_origin, store: store, origin: 'https://shop.example.com')+ it 'stays pending even when an allowed origin exists' do+ expect(subject).to be falseend-- it { is_expected.to be false }- end-- context 'when only a loopback origin exists' do- before do- create(:api_key, store: store, last_used_at: 1.hour.ago)- create(:allowed_origin, store: store, origin: 'http://localhost')- end-- it { is_expected.to be false }- end-- context 'when connected via the storefront_url preference only' do- before do- create(:api_key, store: store, last_used_at: 1.hour.ago)- store.update!(preferred_storefront_url: 'https://app.example.com')- end-- it { is_expected.to be true }endendend