SSR / foldkit-host
Built once per isolate
A Worker's isolate outlives its requests. Use Host.isolate to initialize
shared services (such as auth services over D1, a template, or an SSR entry) on
the first request that needs them:
ts
interface Database {
readonly prepare: (sql: string) => unknown
}
interface AppEnv {
readonly DB: Database
readonly ORIGIN?: string
}
const servicesOf = Host.isolate(
(env: AppEnv) => env.DB,
async env => ({ db: env.DB, startedAt: Date.now() }),
)
export const answer: Host.Handler<AppEnv> = async (_request, env) =>
new Response(String((await servicesOf(env)).startedAt))Host.isolate keys its cache in a WeakMap on the binding returned by the
first callback. When local dev servers like Miniflare hand out fresh bindings on
reload, old entries are bypassed and garbage-collected automatically.
- A build that throws or rejects is cleared, so the next request retries while concurrent callers wait on and receive the same failure.
- Each
Host.isolatecall keeps its own cache, so two isolates over the same binding stay independent. - Build long-lived Effect services into a
ManagedRuntimehere instead of callingEffect.provideper request, which closes the layer after every response.