52

Headless Commerce on Next.js Commerce and Swell

How the Park & Pick storefront plugs into a headless commerce backend. It starts from Vercel's Next.js Commerce monorepo and its provider abstraction, and switches from the local mock provider to Swell with hosted checkout.

Overview

This is the engineering side of Park & Pick: the headless commerce architecture underneath the storefront. Instead of building a shop and a backend from scratch, I started from Vercel's open-source Next.js Commerce monorepo and wired it to Swell as the commerce backend. The architecture makes that swap a configuration change rather than a rewrite.

The provider abstraction

Next.js Commerce separates the storefront from the commerce platform behind a common interface, @vercel/commerce. The monorepo (pnpm workspaces with Turborepo) ships one package per platform, including Shopify, BigCommerce, Saleor, Swell and a local mock. Each implements the same hooks and API operations: products, cart, search, wishlist, customer and checkout.

The storefront imports @framework, a TypeScript path alias that points at exactly one provider. Switching providers means changing that alias and the matching environment variables. The UI code doesn't change. In this project I moved the alias from the local mock to the Swell package, which turns a demo into a real shop.

Request flow

  • Catalogue and search are fetched through the provider's operations and rendered by Next.js pages.
  • Cart and wishlist operations go through a single catch-all API route that dispatches to the active provider's handlers, so the browser never talks to the commerce platform directly.
  • Checkout is delegated. /checkout is rewritten to the commerce API, which hands off to Swell's hosted checkout. Card data stays with the payment platform, and so does PCI scope.
  • Feature flags in commerce.config.json switch capabilities per deployment. Here cart, search and wishlist are on; custom checkout and customer accounts are off.

Why headless

A small team gets inventory, pricing, orders and payments from a managed platform and keeps control of performance, branding and the shopping experience. The provider abstraction is also an exit strategy: if the commerce platform stops being the right choice, the storefront survives the migration.

Stack

  • Next.js 12 (Pages Router), React 18, TypeScript, Tailwind CSS
  • @vercel/commerce provider architecture, Swell
  • pnpm workspaces, Turborepo
  • Internationalised routing (en-US, es)
  • Vercel
Home
About
Projects
Blog
Journal
Contact
Fun
Certifications
Copy email address
Open interactive résumé
Open GitHub
Open LinkedIn
Subscribe to the blog