One build, many sites

One build, many sites

barakoCMS D22 reverses "identity is build time" for the shared renderer: every site runs the same image, and what makes a site that site is its tenant's site settings entry, edited in barakoBrew.

export const config = defineConfig({ sites: {} });

A request-time site puts its page routes under one tenant segment and adds a proxy.ts beside app/:

proxy.ts                              export default createPressProxy(config)
app/
  %5Fpress/[site]/                    the tenant segment. %5F is `_`, which Next reads as a
    layout.tsx                        private folder when it is written plainly
    page.tsx
    [...path]/page.tsx
    blog/[slug]/page.tsx
    %5Flink/[[...path]]/page.tsx      what a link to one entry or one page opens; never kept
  feed.xml/route.ts                   these resolve their own tenant and stay where they are
  robots.ts
  sitemap.ts
  api/...
  %5Fshare/route.ts
// proxy.ts
import { createPressProxy } from "barakopress";
import { config } from "@/press.config";

export default createPressProxy(config);

The proxy resolves the tenant and the share session once and rewrites to /_press/<tenant>~<gate>~<host>/<path>. The pages read all three out of that segment, so nothing in a page reads a header or a cookie, and Next can keep the render. Why the tenant has to be in the path, and what is cached and what is not, is The render cache. A build-time site needs none of this and keeps its routes where they are.

On each request the engine resolves the tenant, reads that tenant's settings, and renders with them:

  1. CMS_TENANT (or tenant) set: every host is that tenant, with no lookup.
  2. sites.tenantHeader set and the request carries a valid handle in it: that tenant. Off by default.
  3. The host, from sites.hostHeader (default host), looked up with GET /api/tenants/by-host/{host}.
  4. sites.defaultTenant, or CMS_DEFAULT_TENANT, read at request time.
  5. None of those: a 404. Never another tenant's site.

A tenant barakoCMS refuses to serve is no site either. On a CMS in Tenancy:Mode Multi, a request for a tenant with no record, or one that is not Active (Suspended or Archived, barakoCMS 5.0), answers 404 with the plain text body This deployment serves registered tenants only.. When the settings read gets that answer, every page, the feed, the sitemap and robots.txt answer 404. Any other 404 on the settings read is a tenant with no settings type yet, and it renders with the configured identity as before. A CMS in Single mode answers a paused tenant with an empty 404, which says nothing about why, so there a paused tenant still renders with the configured identity and 404s inside it. A 404 also drops the last good answer kept for that read and the tenant a host last resolved to, so an outage after the tenant was paused cannot bring its pages back. A paused tenant's domain is an unknown host to the lookup, so it falls to sites.defaultTenant when one is set, the same as any host the CMS does not know.

A handle is only ever read from the request through a header the operator named. X-Tenant and X-Forwarded-Host sent by a caller are ignored unless you configure them, and you should configure them only behind a proxy that sets the header and strips a caller's value.

The settings are the singleton site type from barakoCMS docs/site-settings.md (POST /api/content-types/blueprints/site, then publish its one entry). The engine reads Name, Tagline, Url, Locale, Logo, LogoAlt, FooterLogo, Favicon, ShareImage, Copyright, Colors (the theme slots), Fonts (a family name per role, and the stylesheet that loads it), Radii, Layout, Space and Text (the spacing and type scales), Tokens and Tones (see Tokens and tones), StyleRecipes (see Style recipes), TopBar, HeaderLinks, MenuLinks, HeaderActions, FooterColumns, SocialLinks, HeaderPath, HeaderTone, FooterPath, FooterTone, AssetsAsSupplied, LogoAsSupplied, LogoClearSpace, PageSizes, ReservedSlugs, Labels, HomePath, HomeCollection, Currency, TimeZone, Robots, EmbedHosts, AnalyticsScript and AnalyticsWebsiteId (see Analytics below), Presets, Plugins (see Plugin packages), and Mode, HoldingPath and HoldingMessage (see Holding mode below). Collections, OptionStyles and OptionColors are read as Collections describes. Variants are not rendered yet. Every value is checked for shape; one that fails, and any the entry leaves out, keeps the configured value, so a half-filled theme renders. A link is a path on the site or an absolute http or https URL. Set Url: without it the feed and sitemap fall back to the host the tenant was found by. A link in HeaderLinks, TopBar or FooterColumns may also carry badge, a marker of up to 12 characters drawn beside the label, and external: true, which the built-in header does not need (every header link is already a plain anchor) and a theme can read.

TimeZone (or timeZone in the config) is an IANA name such as Asia/Manila. Dates, from the byline to a date binding, show in that zone. Without it a timestamp follows the server's zone, so a host in UTC can put an evening event on the day before. A bare YYYY-MM-DD names a calendar day, so it formats in UTC either way and keeps its day. A name the runtime does not know is ignored. Robots (or robots in the config) is { "disallow": ["/api/"] }. Each path is added as a Disallow line next to Allow: / in the generated robots.txt. An entry that is not a string starting with / is dropped, which includes anything with a newline or other control character, and the list is capped at 100 entries. Dropped entries are said once in the server log. An empty disallow in the site setting clears the config list; a list whose entries were all dropped falls back to it. A holding site keeps Disallow: / whatever is set, and a site with no Robots gets the same file as before. A link in HeaderLinks or MenuLinks may also carry activeOn and children; see The built-in header.

createSiteLayout and createSiteMetadata render the root layout from all of this: lang, the faces, the palette, the top bar, header links, footer columns, social links and the copyright line.

What the site serves at /. Every site used to be a blog at the root, because the root route mounted the post index and nothing else could be named:

Field Type What
HomePath string A site path such as /home. The page the Pages module serves there is the home page. Nothing served there falls back to the index, so naming a page before writing it is safe
HomeCollection string The key of a collection. Its index is the home page. HomePath wins when both are set

Neither set, / is the post index, which is what it was. The blog's own post, author and category are ordinary Collections entries now, so a school whose news lives in article with a Headline replaces post in its settings and gets both its list and its item pages from that entry. The RSS link in the built-in header, and the feed alternate in the page metadata, appear only when some collection has feed on, so a clinic with no posts stops advertising an empty feed.

The words a visitor reads. Labels is the visitor-facing copy, key by key. A school setting Locale to fil-PH used to get Filipino dates beside English "min read" and "Related":

{ "Labels": { "minRead": "minutong pagbasa", "by": "ni", "related": "Kaugnay" } }
Key English
minRead min read
by by
related Related
relatedNote cosine similarity, computed on load, not curated
featured Featured
back Back
home Home
preview The banner over a draft being previewed
untitled Untitled
feed RSS
empty, emptyNote The notice on an index with nothing published
failed, failedNote The notice on an index whose read failed
shareInvalid This link is not valid or has expired.
shareTitle Opening a share link: the title of the page a share link lands on
shareNoScript What that page says to a browser that runs no script
shareOpening Opening the site.: what it shows while the link is redeemed
search Search: the label and placeholder on the search box
searchEmpty Nothing matches that.: a search that matched nothing. A tree's search box puts what was typed in place of {query}
previous, next Previous, Next: a tree page's links along the reading order
editPage Edit this page: the link to where a tree page is written
contents Contents: the collapsed tree sidebar on a phone
products Products: the label on a tree's product switcher
openMenu, closeMenu Open menu, Close menu: the header's phone menu button
menu Menu: the name of the phone menu's links
submenu {label} links: the button beside a header link with children, {label} its label
onThisPage On this page: the heading over a tree page's rail
closeContents Close: the closed phone disclosure's control while it is open

A key left out, or saved as anything but a word, keeps the English, so a half-filled map reads. A build-time site passes labels to defineConfig. Nothing about a site's own content is here: a collection's heading is its label and how its count reads is its noun.

The colour slots, by role. Colors sets any slot of the palette, one at a time. The slots are pageBg, surface, ink, proseInk, secondaryInk, muted, hairline, accent, accentHover, accentInk, accentTint, accentTintBorder, accentBorderStrong, inverse, inverseChrome, inverseInk, inverseAccent, code and success. A role says where a colour goes rather than what it looks like: inverse is the band that reverses the page, the footer and a code panel and an inverse block, so a bakery with a cream footer sets inverse to cream and reads right doing it.

{ "Colors": { "accent": "#17458F", "inverse": "#F4E3C1", "inverseInk": "#3B2A17" } }

Six slots shipped in 0.3.0 under barakocms.com's own names, and those still work: darkPanel, darkPanelChrome, darkPanelInk, darkPanelAccent, codeGreen and accentTintBorderStrong are read into inverse, inverseChrome, inverseInk, inverseAccent, code and accentBorderStrong. Setting either name sets both, so a tenant saved before the rename keeps its site and a consumer's own component reading theme.colors.darkPanel keeps compiling. The old names are deprecated and go in 2.0.0. An OptionStyles or OptionColors entry naming an old slot resolves too.

Sizes work the same way. Text is the type scale by role, meta, small, body, lead, subheading, heading, title, display and pageTitle, and Space is the spacing scale. The blocks and the screens read those names, so a tenant that wants bigger headings sets title once instead of asking for a release.

How many items, per tenant. PageSizes sets the four counts for this site, each on its own:

{ "PageSizes": { "index": 50 } }

A bakery listing 50 products and an agency listing 9 case studies run the same image. A key the entry leaves out keeps the configured count, and a collection's own pageSize still wins over the site's. barakoCMS clamps a public list at 100 whatever is asked for, so a larger number is not an error and does not buy more rows.

Paths this site does not serve. ReservedSlugs adds to the configured reserved list:

{ "ReservedSlugs": ["shop", "status"] }

A first segment on it is left out of the menu and the sitemap, and a page there is never asked for. The sitemap lists every published page, not only the menu: the pages in the menu, the internal targets of HeaderLinks, and each page of the page type that answers at /slug. Region pages, a collection's indexPage and noIndex pages are left out. On a build with PRESS_TRAILING_SLASH=true the sitemap and the feed write the trailing slash. It is what a proxy in front of this domain answers instead of the renderer, which is a fact about one tenant rather than about the image. It only ever adds: a tenant cannot free a segment the app's own routes already hold, because a page there would sit behind a route file and render nowhere. A tenant's collection routes need no entry, since those are read off its settings already.

Fonts, from an allow list. A family name on its own is loaded from Google Fonts, which is what it has always meant:

{ "Fonts": { "heading": "Zilla Slab" } }

A site that cannot use Google Fonts, a school with a licensed face on its own host or a tenant that must not send visitor addresses to a third party, names the stylesheet instead:

{ "Fonts": { "heading": { "family": "Zilla Slab", "url": "https://type.school.example/zilla.css" } } }

A serif face needs a serif stack behind it while it loads. Add "fallback": "serif" and the family is followed by Georgia, 'Times New Roman', serif instead of the sans stack:

{ "Fonts": { "heading": { "family": "Source Serif 4", "fallback": "serif" } } }

Leave it out, or set "sans-serif", for the configured stack. Any other value is ignored.

That URL is a tenant's setting on its way into a <link> in every visitor's page, so which origins a page may reach is the deployment's decision and not the tenant's. PRESS_FONT_ORIGINS is the list: origins separated by commas or spaces, each https://host, a bare host read as https.

PRESS_FONT_ORIGINS What a page may link
unset or blank Google Fonts, and nothing else. This is what every site rendered before the list existed
https://type.school.example That origin only. Nothing goes to Google Fonts, the built-in link and its preconnects included
fonts.googleapis.com, type.school.example Both

A URL on any other origin is refused: no link to it is rendered, the role falls back to its family name, and the server log says so once rather than once a page view. The list replaces the default rather than adding to it, which is what gives a tenant that must not reach Google Fonts a deployment where nothing can. createSiteLayout(config, { loadFonts: false }) still turns off every font link for the whole image.

Only an absolute https URL is kept, for the whole chain: http is blocked as mixed content on every site this serves, so allowing it would mean rendering a link that never loads. A role the tenant names is the tenant's, family and stylesheet together, so a family set with no url clears a configured stylesheet rather than leaving the page loading a face it no longer uses. A build-time site sets the same thing in theme.fontSources, and it is held to the same list.

Analytics, from an allow list. A site counted by Umami names the tracking script and its website id, and every page of the site gets <script defer src="..." data-website-id="..."> in its head:

Setting What
AnalyticsScript The Umami script, an absolute https URL
AnalyticsWebsiteId The site's Umami website id, a UUID

Both are a tenant's settings on their way into a script tag in every visitor's page, so which origins a page may run a script from is the deployment's decision and not the tenant's. PRESS_SCRIPT_ORIGINS is the list, written the way PRESS_FONT_ORIGINS is: origins separated by commas or spaces, each https://host, a bare host read as https. Unset or blank, it allows nothing, so a deployment that sets nothing renders the head it rendered before, whatever its tenants have saved.

The tag renders only when the URL is https with no credentials, its origin is on the list, and the id is a UUID. Anything else renders no tag, and a refused origin is said once in the server log. Only the site's own pages carry the tag: the holding page and the document a request with no tenant gets do not. A build-time site sets site.analyticsScript and site.analyticsWebsiteId in its config, held to the same list, and a tenant's settings win over them. On a request-time site that means a tenant that sets neither field reports to the config's website id, so leave them out of a config that serves more than one tenant. This is Umami's script and nothing else; there is no setting that puts other markup in the head.

The tag is part of the cached page, like every other identity field, so a change to either setting reaches visitors on the next revalidation: the webhook that follows publishing the settings entry, or the backstop.

PRESS_SCRIPT_ORIGINS is read when a page renders, not when it is served. A page already cached keeps the tag it was rendered with, so removing an origin, or clearing the settings, reaches a cached page only when that page renders again: on a purge from the webhook, or the backstop. To stop a script at once, deploy with the new list and purge the site's cache tag, or rebuild a build-time site whose pages were prerendered. Content routes stay cacheable on purpose; this is the price.

barakocms.com, as the worked example:

  1. Register the site in Umami, through the console's Analytics page or POST /api/analytics/websites on the barakoCMS API. The answer's snippet holds the script URL and the website id.
  2. Allow the script's origin on the renderer: PRESS_SCRIPT_ORIGINS=https://playground.baryo.dev.
  3. Set both in the tenant's site entry and publish it:
{
  "AnalyticsScript": "https://playground.baryo.dev/analytics/script.js",
  "AnalyticsWebsiteId": "0f6a1c2e-3b4d-4e5f-8a9b-1c2d3e4f5a6b"
}

The id above is a placeholder; use the one Umami gave.

A site that draws its own root layout instead of createSiteLayout's (barakocms.com's theme does) gets no tag from the engine's head. Put <SiteAnalytics site={cfg.site} /> in that layout's <head>. It applies the same checks and the same allow list, and renders nothing when they fail. A look check that captures a page carrying the tag should refuse the script with a block glob such as **/analytics/script.js, so a capture never depends on the tracker answering.

Header and footer as block regions. The built-in header and footer take links and text and nothing else, so a clinic that wants a light footer with opening hours and a map cannot have one, and a school that wants an enrolment banner with a button cannot either. Point a region at a page and its blocks are drawn there instead, resolved and bound exactly as the page route resolves and binds them.

Field Type What
HeaderPath string A site path such as /site/header. The page served there is drawn in place of the top bar and the header band
HeaderTone string page, surface, accent, inverse, gradient, wash, or a name from Tones: the tone behind the header region. page when unset or not one of those
FooterPath string A site path such as /site/footer. The page served there is drawn in place of the footer
FooterTone string The same names, behind the footer region

Set neither and nothing changes: TopBar, HeaderLinks, FooterColumns, SocialLinks and Copyright draw the built-in chrome with the markup they always had, which is what keeps a site whose own CSS keys off that markup rendering. A path with nothing served at it does the same, so naming a page before writing it is safe, and so is a typo.