18 min read
Custom domains
A merchant connects brand.in in Settings → Domains. The row starts pending; the storefront
only answers a hostname once its store_domain row is status = 'active' (resolve_store /
resolve_host, see Request flow). A domain provider decides when that
happens.
Code: packages/core/src/domains/ (service.ts for connect / verify / poll, records.ts for
the records we show, provider.ts for the provider contract, dev-provider.ts and
dns-provider.ts and cloudflare-provider.ts for the providers, cloudflare.ts for Cloudflare’s
API, routes.ts and dokploy.ts for the automatic HTTPS route). Admin:
apps/admin/src/components/domains/.
The records we ask for
| Hostname | Routing record | Ownership record |
|---|---|---|
brand.in (apex) | A @ → STOREFRONT_A_RECORD | TXT _waimers-verify → waimers-verify=<token> |
www.brand.in (subdomain) | CNAME www → STOREFRONT_CNAME_TARGET | TXT _waimers-verify.www → waimers-verify=<token> |
The token is made when the domain is connected and never changes for that row.
With DOMAIN_PROVIDER=cloudflare the table also shows the TXT record Cloudflare returns
(_cf-custom-hostname.www), until it says the hostname is active. It is stored in
store_domain.edge_records because its value is per hostname and the admin must not call
Cloudflare to draw a page.
Providers
DOMAIN_PROVIDER picks one; dev is the default.
dev (offline)
Never touches the network. *.test and *.example verify on the first check (certificate
issuing for a minute, then active), a first label starting with fail is misconfigured, and
every real domain stays pending forever. Right for local work and tests, wrong for any server
where merchants connect real domains.
dns (real)
createDnsDomainProvider in dns-provider.ts. On each check it asks DNS (node:dns/promises,
5 s per lookup) and, once DNS is right, does a TLS handshake.
| Situation | DNS state | What decides it |
|---|---|---|
Apex A records include STOREFRONT_A_RECORD | routing OK | resolve4(host) |
Subdomain CNAME equals STOREFRONT_CNAME_TARGET (case and trailing dot ignored) | routing OK | resolveCname(host) |
Subdomain A record is STOREFRONT_A_RECORD (flattened CNAME, Cloudflare DNS-only) | routing OK | resolve4(host) |
| Every A record is in Cloudflare’s published ranges (orange cloud) | routing OK only if the TXT record matches; noted in error | resolve4 + resolveTxt |
_waimers-verify[.name] TXT holds the token | ownership OK | resolveTxt |
| Records missing (NXDOMAIN / no data) | pending | keeps polling |
| Routing points somewhere else, or the TXT has another value | misconfigured → row failed | plain-words error, e.g. “brand.in points at 1.2.3.4. Change the A record to 15.206.197.145.” |
| A lookup times out or the DNS server fails | previous state, no throw | previous in the check input |
When routing and ownership are both OK the provider connects to host:443 with
servername = host and rejectUnauthorized: true (5 s limit). A trusted certificate makes the
SSL state active; anything else is issuing and the next check tries again.
verifyDomain writes the answer: DNS verified flips the row to status = 'active' (the
storefront starts answering) and copies the SSL state. A live domain never drops back to
pending over a failed check. The resolver and the TLS checker are function parameters, so
tests (dns-provider.test.ts) never touch the network.
cloudflare (Cloudflare for SaaS)
createCloudflareDomainProvider in cloudflare-provider.ts, over the custom hostnames API
(cloudflare.ts). Shoppers on *.koomerce.app arrive through Cloudflare and the tunnel; a merchant
domain with a direct A record skips both, which loses attack protection, leaves
cf-connecting-ip unset, and keeps the origin open. In this mode a subdomain goes through
Cloudflare like a store address does.
A subdomain (www.brand.in) is verified only when all of these hold.
| Check | Why it is not enough alone |
|---|---|
Cloudflare’s own status is active (or active_redeploying) | A TXT-validated hostname turns active before its DNS moves |
A CNAME to STOREFRONT_CNAME_TARGET. A direct A record is refused | Cloudflare’s docs ask for this too: active status, active certificate, DNS on the target |
Our _waimers-verify TXT | The CNAME says nothing about which store. Without this, a CNAME left behind by a store that moved away could be claimed by another |
The custom hostname is created once our TXT is in place, not at connect. Each one is billed past
the included 100 and a pending row is polled for good, so creating on sight would let any merchant
run up the bill with names they do not own. edge_records stays null until then.
The certificate is active only when Cloudflare’s ssl.status is active and Traefik has the
route (route_status = 'ready'): the tunnel ends at Traefik, and Traefik answers 404 for a
hostname it does not know. Until then it reads issuing, which also keeps the poll asking for the
route. No TLS handshake is involved: a padlock only shows that someone has a certificate.
| Cloudflare says | Row | Merchant sees |
|---|---|---|
blocked, pending_blocked, test_blocked, or a held-zone error | failed (live rows keep active, SSL failed) | “This domain is still connected to another host…” (translated; heldElsewhere on the view) |
| Anything else not active, DNS right | pending, and a no-change PATCH asks Cloudflare to look now | “Your records look right. We are waiting for Cloudflare…” |
moved or deleted, DNS wrong | failed | The record to fix. The PATCH that revives it comes when DNS is right again |
| Certificate timed out or expired | verified, SSL issuing, and the same PATCH | “Getting your security certificate” |
A CAA error in ssl.validation_errors | As above | Which record blocks the certificate |
blocked is terminal at Cloudflare until someone asks again, so a check does (the same PATCH), and
those rows are not polled, so only Verify connection repeats it. A rate limit, an auth failure,
an outage, or a failed DNS lookup returns the previous state with a “try again” message, as dns
does for a lookup timeout. The call is logged by status, never with the token or Cloudflare’s text.
Removing a domain deletes its custom hostname through the domain.route_removed job, otherwise it
would stay claimed on our zone, which is exactly the trap Shopify’s leftover hostname set.
The certificate sweep and Retry certificate skip rows with edge_records set: their
certificate is Cloudflare’s, and the sweep’s handshake would pass on a padlock alone.
An apex is redirect-only
Most registrars cannot CNAME an apex, and Cloudflare’s apex proxying is Enterprise only. So:
brand.inkeeps theA @ → STOREFRONT_A_RECORDrecord and the TXT. It uses thednsrules, with one difference: a proxied apex does not count, because only our own address proves the request reaches Traefik. Cloudflare’s API is not called.- Its Dokploy route carries one Traefik middleware,
apex-to-www@file, which redirects tohttps://www.<apex>/<path>before the request reaches the storefront (see the rollout below). - Connecting an apex always brings its www, which is the domain that serves; a www held by another
store refuses the connect.
domains.setPrimaryrefuses an apex. Copy: “brand.in can only redirect, so it cannot be primary. Make www.brand.in your primary domain instead.” - An apex that is already primary keeps serving and gets no middleware. Existing rows are not migrated or edited.
Re-checks
- Worker: the
{domains}-verifyrepeat job (every 2 minutes, scheduler idverify-pending-domains,apps/worker/src/domains.ts) checks every custom domain that ispending, oractivewith a certificate stillpending/issuing(stores_with_pending_domains()). - Admin: the domain page re-checks every few seconds while the tab is open and the domain is
settling, and “Verify connection” checks at once (
domains.verify). - A
faileddomain is not polled: the merchant fixes the records and presses “Verify connection”, as the page tells them.
Turning it on for a server
DOMAIN_PROVIDER=dns
# The storefront server's public IPv4. Apex domains point their A record here.
STOREFRONT_A_RECORD=15.206.197.145
# A hostname that resolves to that server. Subdomains point their CNAME here.
STOREFRONT_CNAME_TARGET=shops.koomerce.apploadEnv refuses DOMAIN_PROVIDER=dns while STOREFRONT_A_RECORD is still the documentation
default (192.0.2.10), so merchants are never told to point at it. Both the admin (the
“Verify connection” button) and the worker (the poll) read these keys, so set them on both.
Each live hostname also needs an HTTPS route on the proxy
DNS reaching the server is not enough: Traefik must route the hostname to the storefront and
hold a certificate for it, and Traefik only does that for hostnames added as a domain on the
storefront app in Dokploy. Until that route exists the TLS check fails and the domain stays
active with SSL issuing.
With the four DOKPLOY_* keys set on the worker, this step is automatic
(packages/core/src/domains/routes.ts):
- The worker’s poll checks a domain, DNS is verified, and the domain has no working
certificate yet. It sets
store_domain.route_status = 'pending'and writes adomain.route_requestedoutbox row in the same transaction (requestDomainRoute). - The outbox relay turns the row into a
{domains}-routejob (6 attempts, exponential backoff from 30 s), keyed by the outbox row so a re-dispatch never doubles it. - The job (
runDomainRouteJob) re-checks the row (still a live custom domain), re-checks the hostname (hostname.tsrules, never a platform domain or the CNAME target), lists the storefront app’s Dokploy domains and creates one only if the hostname is not there yet:https: true,certificateType: 'letsencrypt',path: '/',port: DOKPLOY_STOREFRONT_PORT. Dokploy writes the Traefik config at once (no redeploy), Traefik gets the certificate, and the next poll’s TLS check moves SSL toactive. The row becomesroute_status = 'ready'. - A job that uses all its attempts marks the row
failed. The poll asks again 30 minutes after the last request (ROUTE_RETRY_MS), as it does for apendingorreadyroute whose certificate still has not shown up (a lost job, or a route deleted by hand). - Removing a domain whose route was ever asked for writes
domain.route_removedwith the hostname. Its job deletes the Dokploy domain, unless the hostname is live again (any store,resolve_store).
The www twin is its own store_domain row, so it gets its own route. Admin requests never call
Dokploy: Dokploy being down only delays the route, and the domain page says “Getting your
security certificate” (or, after the retries, that it is taking longer than usual).
| Key | What it is |
|---|---|
DOKPLOY_URL | Dokploy’s base URL as the worker reaches it, without /api, e.g. https://dokploy.example.com |
DOKPLOY_API_KEY | A Dokploy API key. Secret: sent only in the x-api-key header, never logged or returned |
DOKPLOY_STOREFRONT_APPLICATION_ID | The storefront application’s id in Dokploy |
DOKPLOY_STOREFRONT_PORT | The port the storefront container listens on (default 3100) |
Any of the first three unset (local, tests): no route is asked for, every route job is a no-op, and each domain is added in Dokploy by hand as before. Every Dokploy request has a 10 s timeout.
Setting it up on the server
- API key: in Dokploy, open Settings → Profile, find API/CLI, and generate a key. Use an account that can manage domains on the storefront app.
- Application id: open the storefront application in Dokploy. The id is the last part of
the address bar (
…/services/application/<applicationId>). It is also in the app’s General tab next to the API examples. - Port: the storefront runs
next start --port 3100; keep3100unless the app’s own domains in Dokploy use another container port. - Set the four keys in the worker’s environment (Dokploy → worker app → Environment), then redeploy the worker. The admin and storefront do not need them.
- Check: connect a test domain, point its DNS at the server, and within a few minutes the storefront app’s Domains tab in Dokploy lists it with HTTPS on.
Domains added in Dokploy by hand before this was turned on are left alone: the worker only
deletes routes it asked for. A hostname listed in PLATFORM_DOMAINS can never be connected, so
it never reaches Dokploy either; take a domain out of that list before connecting it as a
store’s own.
Other things that keep a certificate from issuing:
- Cloudflare proxy (orange cloud): the handshake reaches Cloudflare, not us, so SSL turns
activeas soon as Cloudflare serves its own certificate. Set Cloudflare’s SSL mode to Full (strict) so it also checks our origin certificate. This is thednsprovider’s blind spot;cloudflaredoes not rely on a handshake. - An extra AAAA (IPv6) record pointing at the old host: Let’s Encrypt prefers IPv6, so the challenge goes there and fails. Ask the merchant to delete it.
- Origin locked to Cloudflare’s ranges (Deployment, “Lock the origin”): a merchant domain that is not on Cloudflare cannot reach the server at all. Servers that take custom domains must allow 80/443 from everywhere, or put the storefront behind a separate entry point.
- The check runs from the server itself (admin and worker containers connect to
host:443). The firewall / security group must let the server reach its own public address on 443, or every domain staysissuing.
Rolling out Cloudflare for SaaS
Everything below is unverified against the real account: it comes from Cloudflare’s, Dokploy’s
and Traefik’s documentation and source. Items marked Confirm are the ones that could turn out
differently. Do them on a test domain before DOMAIN_PROVIDER=cloudflare reaches production.
Order matters: the provider starts creating hostnames (and counting towards the bill) as soon as it is on.
On Cloudflare (koomerce.app)
- Turn on Cloudflare for SaaS on the zone (SSL/TLS → Custom Hostnames). 100 hostnames are included on Free, Pro and Business, then $0.10 each per month. A www counts from the moment its TXT is seen, finished or not. Apex rows do not count. Confirm the plan and billing.
- Fallback origin: a proxied CNAME, for example
fallback.koomerce.app, to<tunnel-id>.cfargotunnel.com. Set it as the fallback origin and wait for Active. Without it every hostname fails validation. - CNAME target:
STOREFRONT_CNAME_TARGETmust be a proxied record on this zone. Point it at the fallback origin. Confirm thatshops.koomerce.appcan move without disturbing what it serves today. - Tunnel: keep the catch-all ingress to Traefik and do not set an HTTP host header on it:
Traefik routes by the merchant’s hostname, so it must arrive unchanged. Confirm with a test
hostname that the Host header survives the fallback, and that Traefik’s answer over the tunnel
works (
noTLSVerify, no SNI for that hostname). - API token: My Profile → API Tokens, scoped to this zone only, permission Zone → SSL and Certificates → Edit. Nothing else. Confirm the dashboard’s label matches.
On the server
-
Dokploy 0.29.0 or later.
middlewaresondomain.createfirst appears in that release. Older releases drop the field silently, so the client checks the answer, deletes the domain and fails the job: an apex is never left serving the storefront. Confirm the running version. -
The Traefik middleware. Create
/etc/dokploy/traefik/dynamic/apex-to-www.yml(its own file, so Dokploy’s writes tomiddlewares.ymlnever touch it). Traefik watches that folder, so no restart is needed. Every apex route shares it, and the router’s exactHost()rule means the first group can only ever be that apex.http: middlewares: apex-to-www: redirectRegex: regex: '^https?://([^/:]+)(?::[0-9]+)?(/.*)?$' replacement: 'https://www.${1}${2}' permanent: truePort 80 is one hop too: Dokploy gives the apex’s
webrouter onlyredirect-to-https, so after adding the route the job swaps it forapex-to-www@filein the storefront app’s Traefik file (application.readTraefikConfig/updateTraefikConfig,domains/one-hop-apex.ts), and every later route job re-applies it. Let’s Encrypt’s challenge is still answered first. A failure is only logged: the apex then takes two hops (http→https→ www). Confirm withcurl -I http://<apex>/a?b=1that the answer is one 301 straight tohttps://www.<apex>/a?b=1. -
Environment. On the admin and worker:
DOMAIN_PROVIDER=cloudflare,CLOUDFLARE_ZONE_ID,CLOUDFLARE_API_TOKEN, realSTOREFRONT_A_RECORDandSTOREFRONT_CNAME_TARGET. The storefront must not get the token. The fourDOKPLOY_*keys are needed on the worker: without them nothing adds the Traefik route, so a domain staysissuing.loadEnvrefuses a missing zone or the default targets. It cannot refuse a missing token, because the storefront loads the same schema, socreateDomainProviderdoes: the worker will not start without it, and the admin’s “Verify connection” reports it. -
Migration
0223_store_domain_edge_recordsruns with the deploy. It adds one nullable column and touches no rows.
First domain
- Connect a test
wwwand add the records. Check the custom hostname appears on Cloudflare once the TXT is in, and watch it reach Connected. Confirm real responses match what the provider reads: the status values,ownership_verification,verification_errorsand thessl.statusvalues. - The case that started this. Connect a hostname another SaaS still holds, with the CNAME
pointing at us. Cloudflare’s docs do not say what happens when two SaaS providers hold the same
exact hostname. The provider handles
blocked, a held-zone error, and a hostname that stays pending. If Cloudflare reports active while the other provider keeps the traffic, this provider cannot see it, and it would say Connected. Confirm this before trusting the badge. If it happens, add a check that fetches the hostname and looks for a storefront-only response. - Connect a test apex. Check
https://<apex>/some/path?a=1answers 301 to the www with the path and query, and that its route in Dokploy shows the middleware.
Existing domains
Nothing moves on its own. A row that is active with SSL active is not polled, so it keeps its
direct DNS until the merchant changes it. For each one: change the record to the CNAME, then press
Verify connection. That first check puts the row on Cloudflare.
A row wrongly marked connected (a false positive from a padlock) needs the same press: it stays live, because a domain never drops back, but the page then tells the merchant what is wrong. A primary apex must move to its www first. After that its route still serves the storefront until someone adds the middleware to it in Dokploy, or removes and reconnects the domain.
The firewall: this design cannot fully close the origin
An apex’s A record points at STOREFRONT_A_RECORD, so that address must answer 80 and 443 for
everyone: visitors for the redirect, and Let’s Encrypt for the certificate. If the same machine is
locked to Cloudflare’s ranges, every apex stops working, and if it stays open, anyone who finds the
address can still reach the storefront directly and send a made-up cf-connecting-ip.
So “close the origin” is only possible once the apex redirect has its own address. The smallest way:
a second small server or static IP running only Traefik with this middleware, with
STOREFRONT_A_RECORD pointing there and the main origin closed (the security audit’s plan of never
opening inbound 80/443 on the new origin). That needs a Dokploy application on that server for
apex routes, which is not built. Until then, leave 80/443 open and treat TRUSTED_PROXY=cloudflare
as not bypass-proof.
Store links use the primary domain
One storefront app serves every store by hostname, so a link must use a hostname that
resolves to the store. STOREFRONT_URL belongs to no store on a multi-tenant server: a link on
it answers “No store is configured for shop.example.in.” Every store-facing link is therefore
built from storefrontOriginFor(tx, storeId, fallback) (packages/core/src/domains/origin.ts):
- the store’s primary domain when it is live (
is_primary AND status = 'active'), ashttps://<hostname>. A primary domain that is stillpendingdoes not count yet; - else
fallback, which isSTOREFRONT_URL(origin only); - in development, when
STOREFRONT_URLis alocalhostURL, the fallback always wins, because the store’s real domains do not point at the dev machine.
Who calls it:
| Where | Links |
|---|---|
Admin pages, through store.origin (apps/admin/src/lib/storefront-origin.ts) | top bar “View store”, theme preview and import report preview, product/page/blog post/policy “View”, Growth › Links |
| API services | marketing shop, click and unsubscribe links (marketingDeps), abandoned-cart recovery link, Settings › Payments test order link, the imported theme editor canvas (Liquid shop.url, theme asset base) |
| Core / worker | order and balance links in order emails (deliverNotification), newsletter confirm link, back-in-stock alert links, campaign and automation sends, ctx.storefrontUrl for apps (storeAppContext) |
| Storefront | storefrontUrl handed to apps’ order links on the thank-you and account order pages |
store.origin is a bare storeProcedure (every member, see
guardrails): it is the address every shopper already sees,
and staff without settings.read still need working links. Theme preview links
(?theme-preview=<token>) work on any hostname of the store: the storefront proxy checks the
token against the store the hostname resolves to and never redirects a preview to the primary.
Checkout itself never reads STOREFRONT_URL: it runs on the shopper’s own host, and gateway
webhooks go to the worker URL. The one payments link the admin builds (Settings › Payments test
order) opens on the store’s origin like the rest.
Certificate checks and retries
Traefik asks Let’s Encrypt for a certificate once, when the route is added. If DNS does not
exist yet (NXDOMAIN) the order fails and Traefik does not try again until the route is reloaded,
so the browser keeps seeing Traefik’s self-signed “TRAEFIK DEFAULT CERT”. The certificate sweep
(runCertificateSweep, packages/core/src/domains/certificate-retry.ts) closes that gap for
platform addresses and custom domains alike.
- The check (
tlsCertState): a TLS connection tohost:443without rejecting untrusted certificates, then inspecting what came back: the default certificate, a chain the system does not trust or a name mismatch, nothing, or a certificate trusted for that host (valid). - The job: a repeat job on
{domains}-certificateevery minute, scheduled unlessDOMAIN_PROVIDER=dev. It looks at rows withstatus = 'active'androute_status = 'ready'that are due (certCheckDue;stores_with_certificate_checks()finds the stores). Waiting rows follow a backoff counted fromssl_started_at: every minute for 5 minutes, then every 5 minutes up to 30 minutes, every 15 minutes up to 2 hours, hourly up to 6 hours, then every 6 hours until 3 days. After that only the merchant’s button works.ssl_checked_atis the last look. Active rows are re-checked once a day, which also catches a row that was marked active without a real certificate. - The outcome, per row (
checkCertificate):
| Seen | Recorded |
|---|---|
| Trusted certificate for the host | ssl_status = 'active', error cleared |
DNS does not resolve to STOREFRONT_A_RECORD (or Cloudflare) | failed, error DNS not pointing here yet |
| DNS right, no certificate, under 5 minutes in | issuing |
| DNS right, no certificate, later | re-register in Dokploy (delete + create, a new ACME attempt), issuing; after a 20 s pause one more look; still none: failed, error Certificate pending |
A re-register needs the Dokploy keys, happens at most once every 10 minutes per host (Let’s Encrypt allows 5 failed validations an hour per host), and only when DNS is already right. A live host that is merely unreachable for a moment is left alone.
- Merchant button: Settings, Domains, the domain, Retry certificate
(
domains.retryCertificate,settings.manage).requestCertificateRetryrefuses an active certificate, a domain whose DNS is not verified, and a second press within 2 minutes; it restarts the wait and writesdomain.route_requestedwithreregister: true. The route job then deletes and re-creates the Dokploy domain and, after the pause, records the result (recheckAfterSettle). The Dokploy keys stay in the worker. - Admin:
DomainView.sslReason(dnsorcertificate) drives the badge (Waiting for DNS / Certificate failed) and the text on the domain page.
Migration 0113_ssl_retry adds ssl_started_at, ssl_checked_at and
stores_with_certificate_checks().
Store addresses (<slug>.<PLATFORM_STORE_DOMAIN>)
A new store’s own address takes the same route path, so with one wildcard DNS record a store works the moment it is created, with no manual step.
- DNS, once: add a wildcard
*A record forPLATFORM_STORE_DOMAIN(for example*.koomerce.app) pointing at the proxy server. Our own hosts (admin, shop, worker, panel…) keep their explicit records, which win over the wildcard. - On creation: when the Dokploy keys are set,
createStoreis told (requestPlatformRoute) to queue the route. The new address row is written withroute_status = 'pending'andssl_status = 'issuing', plus adomain.route_requestedoutbox row, all in the store transaction. Store creation never calls Dokploy, so Dokploy being down never blocks it. - The job (
runDomainRouteJob) accepts a platform row only for<one-label>.<PLATFORM_STORE_DOMAIN>(storeDomainin the route guard), adds it to the storefront app in Dokploy (HTTPS, Let’s Encrypt,DOKPLOY_STOREFRONT_PORT) unless it is already there, and setsroute_status = 'ready'. It does not assume the certificate:ssl_statusstaysissuingand the certificate sweep (below) moves it toactiveonce the host really serves one. Retries as for custom domains (6 attempts, exponential backoff); after that the row staysfailedand the domain page shows “taking longer than usual”. - Reserved names: the store address cannot be one of our own hostnames
(
RESERVED_STORE_SLUGSinpackages/core/src/onboarding/slug-format.ts), so a merchant can never takeworker,panel,dokployand the like.
Not covered: changing a store’s primary address after PLATFORM_STORE_DOMAIN changes. Existing
stores keep their old address rows; a merchant cannot switch to the new one from Settings → Domains.