Multi-zones
Goal: two Hamur apps, built and deployed on their own, answer under one host: / is the front app and /docs/ is the docs app. Next.js calls these multi-zones. The runnable sample is examples/recipes/multi-zones/, a front/ and a docs/ app.
- Give the second app its own base and its own asset folder. Every route, link and file of that app lives below
app.baseURL;app.buildAssetsDirnames the folder of its built client files. Give each app its own, because withapp.cdnURLthe files are at<cdnURL><buildAssetsDir>and two apps on one CDN would write the same paths:
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
app: { baseURL: "/docs/", buildAssetsDir: "/_docs/" },
})
- In the front app, send
/docs/**to the second app with aproxyroute rule, and give the front its own asset folder. The**of the target is what the key matched, so/docs/guide?x=1goes to/docs/guide?x=1of the upstream, and so do the docs app's files below/docs/_docs/. The target is read when the config loads; the sample takes it fromDOCS_ORIGIN:
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
app: { baseURL: "/", buildAssetsDir: "/_front/" },
routeRules: {
"/docs/**": { proxy: `${process.env.DOCS_ORIGIN ?? "http://127.0.0.1:3001"}/docs/**` },
},
})
The upstream sees its own host, hop-by-hop headers are dropped, a redirect is passed on rather than followed, and an upstream that cannot be reached answers 502 (Proxy). The static preset drops proxy rules, so the front app needs a server preset. The request's cookie and authorization headers go to the upstream, so proxy only to an app you run.
- Link across the zones with
external. ALinkwithexternalis a plain<a rel="external">: the navigator never takes its click and never prefetches it, so the browser makes a document request that the front app proxies. A plainLinkis a client navigation inside its own app, and the front app has no page at/docs/. Inside the docs app, aLinktarget is an app path that getsapp.baseURLin front, so a link back to the front app is a plain anchor withrel="external":
import { Link } from "@hamur/adapter-react"
export function FrontNav() {
return (
<nav>
<Link to="/pricing">Pricing</Link>
<Link external to="/docs/">
Docs
</Link>
</nav>
)
}
export function DocsNav() {
return (
<nav>
<Link to="/guide">Guide</Link>
<a href="/" rel="external">
Home
</a>
</nav>
)
}
- Share the session. Both apps seal the cookie with the same
passwordand use the same cookie name, so either one reads what the other wrote. Behind one host the browser sends the cookie to both zones already, and the front forwards it. Setcookie.domainas well when a zone is also reached on a name of its own, such asdocs.example.com; the name is thenhamur-sessionin both, since a__Host-cookie never has a domain (Sessions). Give both apps the sameSESSION_PASSWORD, at least 32 characters, and the sameCOOKIE_DOMAIN:
import { defineHandler, useSession, type HamurEvent } from "hamur/server"
function zoneSession(event: HamurEvent) {
const domain = process.env.COOKIE_DOMAIN
return useSession<{ user?: string }>(event, {
password: process.env.SESSION_PASSWORD as string,
cookie: domain ? { domain } : {},
})
}
export const login = defineHandler({
route: "/api/login",
method: "POST",
async handler(_, { event }) {
const session = await zoneSession(event)
await session.regenerate()
await session.update({ user: "ada" })
return { user: "ada" }
},
})
export const whoami = defineHandler({
route: "/api/whoami",
method: "GET",
async handler(_, { event }) {
const session = await zoneSession(event)
return { user: session.data.user ?? null }
},
})
- Build and run each app.
DOCS_ORIGINis where the front app finds the docs app:
cd docs && hamur build && PORT=3001 SESSION_PASSWORD=... bun .output/server/index.mjs
cd front && DOCS_ORIGIN=http://127.0.0.1:3001 hamur build && PORT=3000 SESSION_PASSWORD=... bun .output/server/index.mjs
POST /api/login on the front app signs in, and GET /docs/api/whoami then answers { "user": "ada" } from the docs app. tests/examples.test.ts runs this and checks that the front app proxies /docs/ pages, their files and the cookie.
Each zone is its own document, so the two never share a page's state: a navigation from one to the other is a full load, and a payload, a store or a loaded route of one zone is not in the other.
See also: Route rules, Navigation, Sessions and security, Configuration.