Serving multiple sites
Available as of version 4.2.1.
A single deployment can serve multiple sites, each with its own content, uploads bucket, and roles. This is useful when sites are created dynamically at runtime, like a subdomain per customer or trial, where deploying a separate server for each site isn't practical.
Each site's data is stored under Projects/<id> in Firestore, just like a single site configured with the id option. The only difference is that the site is resolved per request instead of hardcoded in root.config.ts.
Resolving the site
resolveSite(req) runs once per request and returns the site to serve. For the duration of that request, its id, name, and storageBucket override the static id, name, and firebaseConfig.storageBucket options, and its domain overrides domain in root.config.ts (which the CMS uses for doc URLs and live links). Everything running during the request picks up the resolved site, including the CMS UI, its sign-in page, and any RootCMSClient instances your routes create:
// @/root.config.ts
cmsPlugin({
// Used outside of a request, e.g. by scripts. See below.
id: 'www',
firebaseConfig: {
// ...
},
resolveSite: async (req) => {
const site = await lookupSite(req.hostname);
if (!site) {
return null;
}
return {
id: site.id,
name: site.name,
storageBucket: site.bucket,
domain: `https://${req.hostname}`,
};
},
}),
Only id is required; any omitted fields fall back to your static config. Returning null sends a 404.
Because resolveSite runs on every incoming request (not just CMS routes), make sure it returns a site for every host you serve (including your primary domain) and cache any database lookups.
Creating a site
A site becomes active as soon as its Projects/<id> document has a roles map (the same map managed on the Settings page). To provision a site from your own code, such as in a sign-up flow, write that document using the plugin's Firestore instance:
const cmsPlugin = getCmsPlugin(rootConfig);
const db = cmsPlugin.getFirestore();
await db.doc(`Projects/${siteId}`).set({
roles: {'owner@example.com': 'ADMIN'},
});
Access and storage
Sites stay isolated through the same security rules used for a single site. Users can only sign in to sites where they have a role, and Firestore and Storage restrict their reads and writes to that site's data.
isUserAuthorized(req, user, site) receives the resolved site as its third argument so you can enforce per-site access rules:
// @/root.config.ts
cmsPlugin({
// ...
isUserAuthorized: async (req, user, site) => {
const account = await lookupSite(site.id);
return !account?.expired;
},
}),
Uploaded files are stored under <id>/uploads/ in the site's bucket. If a site uses a bucket other than your default one, apply the CMS's Storage rules to that bucket as well.
Scripts and scheduled jobs
Outside of an HTTP request, Root.js falls back to the static id option. Use runWithSite() to run code in the context of a specific site, such as a script that seeds content for a new tenant:
const cmsPlugin = getCmsPlugin(rootConfig);
await cmsPlugin.runWithSite({id: 'acme'}, async () => {
const cmsClient = new RootCMSClient(rootConfig);
await cmsClient.saveDraftData('Pages/index', {
meta: {title: 'Welcome'},
});
});
The scheduled job runs for one site at a time. Calling /cms/api/cron.run runs it for whichever site the request resolves to, so schedule a job against each site's host. Note that the cron function exported from @blinkk/root-cms/functions only runs for the static id.