Multi-tenant
Goal: one build answers several domains, each with its own content, canonical URL and name. The runnable sample is examples/recipes/multi-tenant/; acme.test and globex.test are served by the same build.
- List the hosts in
site.tenants. A request whose host (Host, orX-Forwarded-Hostunderserver.trustProxy) is in a tenant'shostsreads that tenant: itsurl,nameandindexableover the basesite. Any other host reads the basesite.site.tenantsis a core key and needs a server preset (SEO):
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
site: {
url: "https://example.com",
name: "Example",
tenants: [
{ hosts: ["acme.test", "www.acme.test"], url: "https://acme.test", name: "Acme" },
{ hosts: ["globex.test"], url: "https://globex.test", name: "Globex", indexable: false },
],
},
})
- Name the tenant in server middleware. A request step can return
rewrite(path)fromhamur/app: the address stays, and the answer is the target page's (Server). The sample sends every page request to/tenants/<host>/…. There is norewriteToand a folder that starts with_is never a route, so the pages live inapp/pages/tenants/[tenant]/. Leave the/_hamur/endpoints and your own handler routes out:
import { rewrite } from "hamur/app"
import { defineMiddleware, getRequestURL } from "hamur/server"
export const tenant = defineMiddleware(({ event }) => {
const { hostname, pathname } = getRequestURL(event)
if (pathname.startsWith("/_hamur/") || pathname.startsWith("/api/")) return
return rewrite(`/tenants/${hostname}${pathname === "/" ? "" : pathname}`)
})
List it in server/index.ts as export default defineServer({ middleware: [tenant] }). / of acme.test renders tenants/[tenant]/index, and /about renders tenants/[tenant]/about, both with params.tenant set to acme.test. A request for /tenants/acme.test from outside is rewritten again to /tenants/acme.test/tenants/acme.test and answers 404, so a visitor cannot ask for another tenant's tree.
- Read the tenant in the page.
useSiteConfig()of the adapter returns the site of the request's host, so a page shows its tenant's name and canonical URL without knowing the list; the canonical link,og:url, the sitemap and the robots file of the SEO module follow it too. A page that needs more than that, such as its tenant's theme or data, readsparams.tenantin itsload:
import { Link, useRoute, useSiteConfig } from "@hamur/adapter-react"
export default function TenantHome() {
const { params } = useRoute()
const site = useSiteConfig()
return (
<main>
<h1>{site.name}</h1>
<p>{`Host ${String(params.tenant)}, canonical ${site.url}`}</p>
<Link external to="/about">
About
</Link>
</main>
)
}
Links between tenant pages use external. The pages sit at /tenants/[tenant]/… but the address is /about, which no page has, so a client navigation would find nothing; external makes it a document request and the middleware rewrites it. A mapping the browser should apply to its own navigations belongs in a rule or the reroute file (Rewrites).
- Cache by host. Every default cache key of Hamur holds the host: a route rule's
swrandcache, aGEThandler'scacheand the fetch cache.acme.test/aboutandglobex.test/aboutare two entries, so one tenant never gets the other's page. The cached page also runs without the visitor's cookie, so a page that depends on the signed-in user needsvaries: ["cookie"](Route rules in the cache):
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
routeRules: { "/tenants/**": { swr: 60 } },
})
- Run it. Send the host you want to test with
curl, since both names point at the one server:
hamur build
PORT=3000 bun .output/server/index.mjs
curl -H 'host: acme.test' http://127.0.0.1:3000/
curl -H 'host: globex.test' http://127.0.0.1:3000/
Each answers its own heading and canonical URL. The prerender and the static preset use the base site only, and a tenant's request on a server preset never takes a prerendered file; the build says how many paths now render on demand.
See also: SEO, tenants, Server middleware, Route rules, Storage and cache.