One storefront app for every shop domain: how we made a shop's look data instead of code
The problem
We build Basklee, a platform for online stores. A shop owner picks a template, connects a domain, and the shop is live. We wanted that to work without one deployment per shop.
The obvious way breaks quickly. One build per shop means hundreds of builds to update, and every fix has to be deployed hundreds of times. One codebase per template means every bug gets fixed in each template separately, so they slowly drift apart. Both fall apart once you have many shops and more than a handful of designs.
So we set ourselves three rules:
- one running app serves every shop domain;
- a template is data, not a separate set of components;
- an owner's saved design never breaks when the app changes.
Idea 1. The host name decides which shop to show
Every request carries a host name. The app asks the backend which shop owns that host, keeps the answer, and refreshes it once a minute for each host. Everything after that, such as the catalog, prices, language and design, belongs to that shop.
A preview address and the shop's real domain show the same content. Only the real domain may be indexed by search engines; preview addresses are marked noindex.
Idea 2. A template is a pack of values plus chosen variants
The base design has 126 components. None of them uses a fixed color, font size or spacing. They read named design values, and a template is a pack of those values.
Values alone give you recolors, and our first three templates looked like recolors. The owner's verdict was blunt: "not distinct at all". So each component got listed variants: header layouts, the way the home page opens, product cards, how headings look, where catalog filters sit (side, top, a bar, a pop-up). A template is a choice of variants plus a pack of values. Now there are seventeen templates, and they differ in structure, not just in palette. They still use one set of components, so a bug fixed once is fixed in all seventeen.
The catch: a new idea for one template has to become a variant that any template could pick. That is slower to build the first time, but nothing gets copied.
Idea 3. The owner's design is a versioned document
What the owner changes in the design editor (sections, blocks, texts, which pages exist, sticky header or not) is saved as one document with a format version. The format has changed twenty times. The app reads every version, so a shop saved under version 3 still opens correctly today.
Two rules keep that safe:
- A format change goes live storefront first. The storefront learns to read the old format and the new one, and only then do the backend and the editor start writing the new format. No shop ever has a document the app cannot read.
- A shop with no saved design must look exactly as before. We learned this the hard way. After one deploy, every shop that had never opened the editor lost its style, because it had no saved design to read the template from. Now, after every deploy, we open one live shop that has no saved design.
Idea 4. The bug: the first POST after a pause never got an answer
Shops worked, except for something strange. The first POST to a shop after about a minute of quiet timed out. The next one took 0.3 seconds. The backend's log had no line for the slow one at all.
The cause was in our own server middleware. To find the shop for a host, it wrapped the incoming request in a standard web Request object and passed that to the lookup. For a POST, creating that wrapper starts reading the request body. Most of the time the lookup answered at once from memory, so nothing went wrong. But once a minute per host the lookup had to wait for the backend. While it waited, the wrapper used up the body, so the proxy behind the middleware got a request with no body left. It waited for a body that never came.
That explains each symptom: once a minute (the refresh interval), only POSTs (GET has no body), and nothing in the backend log (the request never got there).
The fix: the middleware now passes only the headers and the client address to the lookup. It never wraps the request.
The rule we keep: a server middleware that awaits anything must never touch the request body, not even by wrapping it.
What to expect, honestly
Variants take more work up front. A one-off design is faster to draw than a variant every template can use.
Old formats stay forever. Reading twenty versions of the design document is code we keep for good.
A cached host answer can be up to a minute old. A new domain or a moved shop takes up to a minute to show.
Have you run many sites from one app? How do you keep a design format readable after years of changes, and have you been bitten by middleware that consumed a request body?
All rights reserved