Storefront setup
Deployment
Deploy the storefront to Cloudflare Workers with OpenNext or to any Node host, with the right environment, proxy and SITE_URL settings.
The storefront deploys to Cloudflare Workers through OpenNext, which is the setup the repository ships with, or to any host that runs Node 22. Either way it is a single app with no database of its own: it needs its environment variables and a route to your WordPress site, nothing else.
Production environment
Section titled “Production environment”Whatever the host, a production storefront needs these at minimum:
COMMERCE_SOURCE=woocommerceWOO_STORE_URL=https://cms.example.comRSC_API_URL=https://cms.example.com/wp-json/retail-store-companion/v1AUTH_SECRET=your-generated-secretSITE_URL=https://shop.example.comSTOREFRONT_SHARED_SECRET=same-value-as-RSC_STOREFRONT_SHARED_SECRETREVALIDATE_SECRET=same-value-as-the-Cache-purge-secretWOO_CONSUMER_KEY=ck_your_keyWOO_CONSUMER_SECRET=cs_your_secretAdd the look and behaviour variables you chose in Configuration. Treat AUTH_SECRET, STOREFRONT_SHARED_SECRET, REVALIDATE_SECRET and WOO_CONSUMER_SECRET as secrets. Without REVALIDATE_SECRET the storefront refuses WordPress’s change signals, and cached pages keep what they were first built with (see Page cache).
Deploy
Section titled “Deploy”The app builds into a Cloudflare Worker with OpenNext. The Worker’s settings are in apps/storefront/wrangler.jsonc: its name (retail-store-frontend), the nodejs_compat and global_fetch_strictly_public compatibility flags, the static assets binding and observability.
The scripts live in the app’s package.json, so run them from apps/storefront:
| Script | What it does |
|---|---|
pnpm cf:build |
Builds the Worker into .open-next/ |
pnpm cf:preview |
Builds, then runs the Worker locally in Cloudflare’s runtime |
pnpm cf:deploy |
Builds and deploys a new version |
pnpm cf:upload |
Builds and uploads a version without making it live |
pnpm cf:typegen |
Regenerates the Cloudflare environment types |
-
Sign in to Cloudflare from the app folder.
Terminal window cd apps/storefrontpnpm exec wrangler login -
Check the build locally.
Terminal window pnpm cf:preview -
Deploy.
Terminal window pnpm cf:deploy -
Set the environment variables in the Cloudflare dashboard, on the Worker’s Settings → Variables and secrets. Store
AUTH_SECRET,STOREFRONT_SHARED_SECRET,REVALIDATE_SECRETandWOO_CONSUMER_SECRETas the Secret type, not as plain text: plain variables show on the settings page and in every build log. Saving deploys a new version with the values. -
Add your domain on the Worker’s Settings → Domains & Routes, then set
SITE_URLto that address.
Variables live in the dashboard, not in git. wrangler.jsonc sets keep_vars: true, so a deploy keeps the dashboard’s variables instead of wiping them. The trade-off is that they are not versioned: set them up by hand for every environment, and keep a record of what each one holds.
Continuous deployment runs on Cloudflare Workers Builds, connected to the repository. The Worker name in wrangler.jsonc must match the Worker the Workers Builds project deploys to. If they differ, CI deploys to its own name and offers a pull request to reconcile, and a local pnpm cf:deploy would update a different Worker. Variables that start with NEXT_PUBLIC_ are compiled in at build time, so give the build step those values too. On the Worker’s Settings → Builds, the Production branch is the git branch whose pushes deploy this Worker (staging for the staging Worker); every other branch and pull request gets a preview build, which runs the non-production build and deploy commands. The repository ships no previews block for wrangler preview, so either set the non-production commands to the same OpenNext build with wrangler versions upload, or disable preview builds for the Worker: left at the defaults, every pull request shows a failed Cloudflare check that means nothing about the code.
A second Worker (staging, or a second store) is an env block in wrangler.jsonc: env.staging is already there. Wrangler uses an env block only when the deploy names it, so set that Worker’s Workers Builds Deploy command to pnpm exec opennextjs-cloudflare deploy --env staging. Without the flag, Wrangler deploys the top-level settings under the second Worker’s name. Its page builds then call the production Worker through WORKER_SELF_REFERENCE, and its own cache stays empty. The production Worker deploys the top level and needs no flag. After the first deploy, and whenever the Worker is connected to another repository, check the Worker’s Settings → Bindings: WORKER_SELF_REFERENCE must name the Worker itself, and the build log must not say “no target environment was specified”.
Any server, container or platform that runs Node 22 or newer works: a VPS, Docker, or a managed Node service.
-
Install on the server, from the repository root.
Terminal window corepack enablepnpm install --frozen-lockfile -
Provide the environment: an
apps/storefront/.envfile, or the service’s own environment settings. -
Build. Variables that start with
NEXT_PUBLIC_must be set at this point, because they are compiled in.Terminal window pnpm build -
Start. The server listens on port 3000; set
PORTto change it.Terminal window pnpm startPORT=3001 pnpm start -
Keep it running under a process manager such as systemd, PM2 or your platform’s equivalent, behind a reverse proxy that terminates HTTPS.
Change a variable, then restart the server. Change a NEXT_PUBLIC_ variable, then rebuild and restart.
Page cache
Section titled “Page cache”The storefront keeps its pages in a page cache, so most visits never reach WordPress. WordPress tells it when something changed, and it rebuilds what that reached.
- Pre-built Pages are built before any shopper asks, after every deploy and again as soon as a change empties one. These are Home, the category index, the content and policy pages, every product page, the blog and every post, each listing’s first page in its common sorts and layouts, and the brand directory.
- On-visit Pages are cached by their first visitor and deleted every night at 04:00, site time. These are later listing pages and the other filtered views.
- Never cached: cart, checkout, account pages, free-text search, typed price ranges and filters with several values.
Four things keep it fresh:
- Change signals. When you save a product, a setting or a page, the Companion plugin signs a signal with the Cache purge secret and sends it once, 15 seconds after the save, on WordPress’s next background run. The storefront checks it against
REVALIDATE_SECRET. Both must hold the same value, and WordPress needs the one-minute cron job, or every signal waits. The storefront rebuilds every pre-built page the signal emptied: for a product, its own page, Home, the category index and the first pages of its listings. - The hourly check. Every ten minutes the storefront looks at a sixth of its pre-built pages, so every one of them each hour, and rebuilds any it finds empty, whatever emptied it. A page that is already cached costs one cached answer.
- The Daily Purge, at 04:00 site time. It brings the listings’ best-selling and top-rated order up to date, rebuilds the pages that depend on it, and clears the On-visit Pages.
- A freshness ceiling (
PRODUCT_PAGE_FRESHNESS_SECONDS, 25 hours by default). It is the backstop for a night whose purge never arrives.
After a deploy the cache starts empty, because entries belong to their build. On Cloudflare the Storefront Clock, a Cron Trigger that runs every minute, starts the new build’s pre-build within a minute, whatever made the deploy, and carries it on to the end. It needs nothing set up, but it is registered only by a real wrangler deploy, not by wrangler versions upload. On a Node host the Companion plugin’s ten-minute Heartbeat starts it. Until the pre-build reaches a page, that page’s first visitor waits for it to render. Every run first checks that the address it builds at answers as this build, and builds nothing if another one answers: the Caching tab then lists a self-check: failure. The pre-build’s progress shows on StoreFront → Settings → Caching, under Purge and rebuild everything. That button empties the cache and rebuilds every page: use it after a long outage, or a change made straight in the database.
The pieces are bound in wrangler.jsonc:
- an R2 bucket,
retail-store-frontend-cache(bindingNEXT_INC_CACHE_R2_BUCKET), for the pages and the data behind them. It must exist in the account before the first deploy. - three Durable Objects, created on the first deploy: the tag cache, which carries a signal to every copy of the Worker; the revalidation queue; and the cache purge, which sends a signal’s purges on to every Cloudflare data centre.
- the
WORKER_SELF_REFERENCEservice binding, through which the Worker builds its own pages. It must name the Worker itself.
Each Cloudflare data centre also keeps its own copy of every page it has served, for about a day, so a visitor is answered from nearby. The cache purge is what clears those copies when a signal purges a page; without it a data centre would build the page again for its first visitor after a change, however fresh the rebuilt page already was. It needs two Secrets on the Worker, CACHE_PURGE_ZONE_ID and CACHE_PURGE_API_TOKEN (see Environment variables), and works only on a custom domain. Until they are set, the pre-build log’s header says Cache purge off. To add them, in the Cloudflare dashboard:
- The zone ID. Open the storefront’s domain (the zone) → Overview. In the right-hand column, under API, copy Zone ID.
- The token. Manage account → Account API Tokens → Create Token → Create Custom Token. Name it for the Worker; give it one permission, Zone → Cache Purge → Purge; under zone resources choose Include → Specific zone and that domain; leave the rest empty. Continue to summary → Create Token, and copy the token: it is shown once.
- On the Worker. Workers & Pages → the Worker → Settings → Variables and Secrets → Add. Type Secret, name
CACHE_PURGE_ZONE_ID, the zone ID as its value; again forCACHE_PURGE_API_TOKENwith the token. Save and Deploy. Both must be the Secret type: builds keep secrets, and nothing shows them on the settings page or in build logs. - The purge window, on the Free plan. On the same page, Add variable: key
NEXT_CACHE_DO_PURGE_BUFFER_TIME_IN_SECONDS, value15, the Secret switch off, then Add variable and deploy. The Free plan allows five purge requests a minute per account; with the default 5-second window a burst of edits can run past it, and with 15 seconds a Worker sends at most four a minute. See Environment variables.
Each Worker (production, staging) needs its own pair, for its own domain. The next time the Worker deploys, the pre-build log’s header says Cache purge on.
A second Worker (staging, a second store) needs its own env.<name> block in wrangler.jsonc, with its name, bindings and a WORKER_SELF_REFERENCE naming itself, and its deploy command must name it: pnpm exec opennextjs-cloudflare deploy --env <name>. Without --env, Wrangler deploys the top-level settings under the new name, and the Worker’s pre-build builds production’s pages. Store REVALIDATE_SECRET, AUTH_SECRET, the WooCommerce key pair and the two cache purge values as Secrets, not plain variables. After the first deploy, and whenever the Worker is connected to another repository, check:
- Settings → Bindings shows
WORKER_SELF_REFERENCEnaming this Worker; - Trigger events lists the cron, every minute;
- the build log has no “no target environment was specified” warning;
- within a few minutes the Caching tab shows the pre-build finished, with nothing failed and no
self-check:line; - the downloaded pre-build log’s header says
Cache purge on.
A self-check: line on the Caching tab means WORKER_SELF_REFERENCE reaches another Worker: see Troubleshooting.
Two Workers on one bucket must set different NEXT_INC_CACHE_R2_PREFIX values. Otherwise one Worker’s nightly purge and its clean-up of old builds delete the other’s pages. Serve the storefront from a custom domain, and switch the Worker’s workers.dev address off (Settings → Domains & Routes): on a *.workers.dev address Cloudflare’s per-data-centre cache doesn’t work, every page read goes to R2, a purge never reaches it, and anything that finds the address (crawlers, a signal from a site that should not have the secret) talks to the Worker past the zone’s rules. Listing addresses can mint many On-visit entries, so put a rate limit on /shop, /category/*, /brand/* and /tag/* at the zone.
With one server, Next’s file-system cache under .next/cache works as it is. The nightly purge retires the On-visit Pages there but leaves their files on disk. Several servers would each keep their own cache, and a signal would reach only the server it lands on. The repository sets up no shared cache handler (typically Redis), so run one server.
Behind a proxy
Section titled “Behind a proxy”Most deployments sit behind something: Cloudflare, a load balancer or nginx. Two things need to be right.
- Sign-in must see the public host. Set
AUTH_TRUST_HOST=trueso Auth.js trusts the forwarded host rather than the address the app sees internally. Do the same behind a tunnel. - The shopper’s IP must arrive in
X-Forwarded-For. WithSTOREFRONT_SHARED_SECRETset, the storefront signs the firstX-Forwarded-Foraddress (orX-Real-IP) and sends it to WordPress so payment rate limits count shoppers. The proxy must set this header, not append to a value the visitor sent, or a visitor could choose their own address. Cloudflare sets it for you.
For nginx in front of a Node host, that looks like:
location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Real-IP $remote_addr;}SITE_URL
Section titled “SITE_URL”SITE_URL is the storefront’s public address, such as https://shop.example.com, without a trailing slash. The storefront never trusts the request’s Host header for this. It builds from SITE_URL:
- canonical addresses and the links between Locales (hreflang),
- Open Graph and X sharing tags,
/sitemap.xml,/robots.txtand/llms.txt.
Only the origin is used; a path after the domain is ignored. In development it defaults to http://localhost:3000 (or your PORT). In production there is no default: unset or invalid, pages print no canonical address, sharing address or language links, and the server log warns once. Set it on every production and staging deployment, to that deployment’s own address.
Staging
Section titled “Staging”A staging storefront should look like production and touch nothing real.
- Connect it to a staging WordPress with payment gateways in test or sandbox mode, never to the live store.
- Set
STOREFRONT_CUSTOM_SCRIPTS=offso test visits never reach live analytics or ad pixels. - Give it its own
SITE_URL,AUTH_SECRET,REVALIDATE_SECRETand WooCommerce REST key pair. - On Cloudflare, deploy it as its own Worker with
--env staging(see A second Worker under Deploy), and give it its ownNEXT_INC_CACHE_R2_PREFIXif it shares the production bucket. - Set the staging WordPress’s Storefront URL to the staging storefront, so emails, preview links and change signals go there. One WordPress sends its signals to one storefront only.
- Put staging behind access control (Cloudflare Access, HTTP authentication at the proxy) so search engines and the public stay out.
- Card wallets (Apple Pay, Google Pay) need HTTPS and a domain registered with Stripe. The Companion plugin registers the Storefront URL automatically, but never local or test hosts.
Continuous integration
Section titled “Continuous integration”The repository’s GitHub Actions check every push to develop and main and every pull request. They do not deploy; Cloudflare Workers Builds does.
| Workflow job | What it runs |
|---|---|
checks |
pnpm typecheck, pnpm lint, a right-to-left styling guard, pnpm test |
smoke |
The full Playwright smoke suite in Demo Mode |
family-smoke |
The design-bearing tests, once per Template Family |
locale-smoke |
The language tests on an English and Arabic store |
address-legacy-smoke |
Checkout and account tests without the Address Book |
offline-only-checkout-smoke |
Checkout tests with only offline payment methods |
A second workflow, WooCommerce contract (payment), runs the contract suite against a real, disposable WooCommerce store every Monday at 03:00 UTC and on demand. It places test orders, so it needs its own store and the repository secrets WOO_CONTRACT_STORE_URL, WOO_CONTRACT_CONSUMER_KEY, WOO_CONTRACT_CONSUMER_SECRET and WOO_CONTRACT_STRIPE_SECRET_KEY. See Testing.