# Isoloop docs > Event-loop isolation for JavaScript apps. An open-source TypeScript library: each part of your app runs on its own worker or in a shared pool, talks over a message bus, and is restarted when it fails, while the rest keeps running. --- Source: https://isoloop.dev/docs/getting-started.md # Getting started This page takes you from an empty Vite app to one isolate running in its own worker, talking through the bus, and restarted when it crashes. ## When to use it Read it first, before any other guide. It sets up the three parts every Isoloop app has: a contracts module, the kernel on the main thread, and isolates. What the page and its servers must send in production is in [Hosting](https://isoloop.dev/docs/hosting.md), and what Isoloop can't do inside an isolate is in [What Isoloop can't hide](https://isoloop.dev/docs/limits.md). ## Install On 0.x a minor release (0.2, 0.3) may contain breaking changes, and a patch doesn't. Only the latest release is supported: see [Limits](https://isoloop.dev/docs/limits.md#only-the-latest-release-is-supported). ```sh npm install @isoloop/host @isoloop/runtime @isoloop/bus zod npm install -D @isoloop/vite # or with pnpm pnpm add @isoloop/host @isoloop/runtime @isoloop/bus zod pnpm add -D @isoloop/vite ``` - **`@isoloop/host`:** the kernel, on the main thread. - **`@isoloop/runtime`:** what runs inside each isolate's worker. - **`@isoloop/bus`:** topics, contracts and the bus worker. - **`zod`:** any [Standard Schema](https://standardschema.dev) library works for the topic schemas. Zod is the one used here. - **`@isoloop/vite`:** the Vite plugin, for development and the build. For a React or Preact isolate, add `@isoloop/react` or `@isoloop/preact`. yarn works the same way (`yarn add`). ## The Vite plugin ```ts // vite.config.ts import { defineConfig } from 'vite'; import { isoloop } from '@isoloop/vite'; export default defineConfig({ plugins: [isoloop()] }); ``` `isoloop()` gives you three things: - **`virtual:isoloop/workers`:** the URLs of the isolate bootstrap and the bus worker, which the kernel starts. - **`?isolate` imports:** `import url from './cart?isolate'` gives a module's URL, to spawn it. In a build, the module is emitted as its own chunk. - **Headers in development:** the Content-Security-Policy from [Hosting](https://isoloop.dev/docs/hosting.md#content-security-policy), with Vite's own needs allowed. Add `/// ` to a `.d.ts` file in your app, so TypeScript knows those imports. ## The contracts The contracts module lists every topic: its name, its schema and how long the bus keeps its records. The host, the isolates and the bus worker all import it, and the bus validates every record against it. ```ts // contracts.ts import { contracts, topic } from '@isoloop/bus'; import { z } from 'zod'; export const cartItems = topic('cart.items.v1', { schema: z.object({ sku: z.string(), qty: z.number().int().positive() }), retention: { kind: 'compacted' }, }); export default contracts(cartItems); ``` A topic's name ends in its version (`.v1`). A change to its schema that old records wouldn't pass is a new topic, `cart.items.v2`. ## The kernel `createKernel` starts the bus worker on the main thread, then starts, watches and restarts each isolate you spawn. ```ts // main.ts import { createKernel } from '@isoloop/host'; import workers from 'virtual:isoloop/workers'; import list from './contracts'; import contractsUrl from './contracts?isolate'; const kernel = createKernel({ contracts: { url: contractsUrl, list }, workers, supervisor: { strategy: 'one-for-one', maxRestarts: 3, withinMs: 60_000 }, }); ``` The options you're most likely to set first: | Option | What it does | Default | | --- | --- | --- | | `contracts` | The contracts module's URL, which the bus worker imports, and its default export, which the host uses | none: without it there's no bus | | `workers` | The isolate bootstrap's URL and the bus worker's, from `virtual:isoloop/workers` | required | | `supervisor` | How crashed isolates restart: `strategy` (`one-for-one` or `never`), `maxRestarts` within `withinMs`, and the `backoff` between tries | one-for-one, 3 restarts in 60 s or 3 in a row after runs shorter than 60 s, 250 ms doubling up to 4 s | | `heartbeat` | When a silent isolate counts as blocked (`blockedAfterMs`) and when it's killed (`killAfterMs`) | 400 ms and 10 s | | `styles` | `CSSStyleSheet`s adopted into every surface | none | | `components` | Host custom elements isolates may render by name, each a `HostComponent`: `{ tag, attributes? }` | none | `createKernel` throws a `KernelError`, whose `code` is a `KernelErrorCode`, when it can't start: - **`unsupported_browser`:** the browser lacks something Isoloop needs. Catch it and show a plain-HTML version of the page ([What Isoloop can't hide](https://isoloop.dev/docs/limits.md#browsers-and-the-first-paint)). - **`invalid_config`:** a malformed option, such as `contracts` without `workers.bus`. The message names it. `kernel.spawn` throws a `KernelError` too, for example `invalid_manifest`, or `bus_failed` once the contracts couldn't load. ## A first isolate An isolate is a module whose default export defines it. This one is a React component, rendered in its own worker and drawn by the host into the element you give it. ```tsx // cart.tsx import { defineIsolate, usePublish, useTopic } from '@isoloop/react'; import { cartItems } from './contracts'; function Cart() { // The latest value. After a restart, it's read again from the log. const last = useTopic(cartItems, { from: 'earliest' }); const add = usePublish(cartItems); return ( ); } export default defineIsolate(Cart); ``` The component can't touch the page's DOM or read layout. It renders into a DOM inside its worker, and the host applies the changes to the page. Layout, focus and scrolling go through the async `ctx.host` calls instead ([What Isoloop can't hide](https://isoloop.dev/docs/limits.md)). ## Spawning it ```ts import cart from './cart?isolate'; kernel.spawn({ id: 'cart', src: cart, surface: document.querySelector('#cart')!, grants: { publish: ['cart.items.v1'], subscribe: ['cart.items.v1'] }, }); ``` - **`surface`:** the element it draws into. Without one, the isolate is headless. - **`grants`:** the topics it may publish and subscribe to. Anything else is refused. - **`pool`:** a name, to share one worker with other isolates instead of having one of its own ([Pools](https://isoloop.dev/docs/pools.md)). ## What a crash does Each isolate runs on its own event loop. When one throws or hangs, the others keep running, and the supervisor handles it: 1. **A crash** (an uncaught error, or the worker dying) restarts that isolate after the backoff. Its state is gone, so it reads what it needs back from the bus: `useTopic(cartItems, { from: 'earliest' })` above gets the latest cart item again. 2. **A hang** shows "Not responding…" over its surface once it misses beats for 400 ms, and is killed and restarted after 10 s. 3. **Past the restart limit** (by default, a 4th crash unless it ran 60 s or more before one of them), it stays down as `failed`, and its surface shows a fallback with a Retry button. ```ts kernel.on('isolate:crash', (e) => console.warn(`${e.id} crashed (${e.reason}): ${e.message}`)); kernel.on('isolate:failed', (e) => console.error(`${e.id} gave up: ${e.message}`)); ``` ## For agents working on your app The `isoloop` CLI carries a "build with Isoloop" skill, in the [Agent Skills](https://agentskills.io) format, that teaches a coding agent contracts, isolates, grants and the limits. Install the CLI (it needs Node 26 or later) and copy the skill into your project: ```sh npm install -D isoloop npx isoloop skill add ``` - **Where it goes:** `.claude/skills/build-with-isoloop/SKILL.md`. For an agent that reads skills from another folder, pass `--dir `. - **After an upgrade:** the skill is the installed CLI's, so run the command again. It doesn't replace a copy that differs, such as one you edited, unless you pass `--force`. For agents without skills, paste this into your app's `AGENTS.md`, so they follow the rules an isolate has to keep: ```md ## Isoloop This app runs its parts as Isoloop isolates, each on its own worker. Docs: https://isoloop.dev/llms.txt - Isolates talk only through bus topics declared in the contracts module, and only on the topics their `grants` allow. Never share state through globals or the page. - A topic name ends in its version (`.v1`). A schema change that old records wouldn't pass is a new topic. - Handlers are idempotent: after a crash the bus can deliver a record again. - Inside an isolate there's no page DOM, no layout and no `localStorage`. Layout reads throw `NotSupportedError`; use the async `ctx.host` calls (`measure`, `focus`, `scrollIntoView`). - An isolate can crash and restart at any time. Keep what must survive in the bus, and read it back on start. ``` ## Limits - **No server-side rendering.** An isolate's content appears once its worker has started and rendered, so anything that must be there at first paint goes in the page's own HTML ([What Isoloop can't hide](https://isoloop.dev/docs/limits.md#browsers-and-the-first-paint)). - **The cost:** every isolate on its own worker adds CPU, memory and start-up time. Pools trade some of that for shared failure ([Pools](https://isoloop.dev/docs/pools.md)). ## See also - [Hosting](https://isoloop.dev/docs/hosting.md): headers, CSP and CORS for production. - [What Isoloop can't hide](https://isoloop.dev/docs/limits.md): what works differently inside an isolate. - [Pools](https://isoloop.dev/docs/pools.md): several isolates in one worker. - [Agents](https://isoloop.dev/docs/agents.md): tools for agents, the dev MCP and WebMCP. --- Source: https://isoloop.dev/docs/limits.md # What Isoloop can't hide What changes for code that runs in an isolate, what Isoloop costs, and what it doesn't protect against, so you can judge whether it fits before you adopt it. ## When to use it Read this page before you move existing code into isolates, or before you pick a third-party library for one. Most of what follows comes from one fact: an isolate runs in a worker, and a worker has no DOM, no layout and no synchronous line to the page. Isoloop's render bridge sends each isolate's changes to the page, which applies them. Code that only builds and updates DOM works as it does on the page. Code that asks the page a question and needs the answer at once doesn't. If none of the limits below stop you, carry on with the [hosting guide](https://isoloop.dev/docs/hosting.md). ## Layout, focus and scrolling are asynchronous The worker has no layout, so it can't answer layout questions synchronously. The DOM an isolate sees leaves these out on purpose, with no fake values: - **Sizes, positions and animations:** these throw a `NotSupportedError` whose message says what to use instead, so a library that reaches for one fails at that line rather than on a `NaN` size later: - the methods `getBoundingClientRect()`, `getClientRects()`, `animate()` and `getAnimations()` on every element, and `getComputedStyle()` on the worker's `window`; - reading `offsetWidth`, `offsetHeight`, `offsetTop`, `offsetLeft`, `offsetParent`, `clientWidth`, `clientHeight`, `clientTop`, `clientLeft`, `scrollWidth` or `scrollHeight`. The members exist, so a feature check such as `'animate' in el` finds them and the call then throws. Other members the worker's DOM doesn't have are absent: calling one throws a `TypeError`, and reading one gives `undefined`. - **Focus:** elements have no `focus()` method, and `document.activeElement` is always `null`. - **Scrolling:** `scrollTop` and `scrollLeft` are read only, as of the last `scroll` event the element got, and stay `0` until the element listens for `scroll`. The page owns scrolling. - **Selection and editing:** there's no `Range`, no selection API and no `contenteditable`. Ask the page instead, through `ctx.host` (`useHost()` in React and Preact). Each call first sends the isolate's pending DOM changes, so the page answers about what you rendered last: | Call | Resolves with | | --- | --- | | `measure(node)` | the node's box relative to the isolate's surface, `{ x, y, width, height }` | | `focus(node)` | nothing, once the page has called `focus()` on the node | | `scrollIntoView(node, { behavior?, block?, inline? })` | nothing, once the page has called `scrollIntoView()` (a smooth scroll may still be moving) | | `requestCanvas(node, { width?, height? })` | an `OffscreenCanvas` for a rendered ``, to draw on from the worker | A failed call rejects with a `HostCallError`, whose `code` says why: `timeout` when there's no answer within 5 s, `not_found` for a node the page hasn't rendered, and `no_surface` in a headless isolate, which has nothing to measure. ```tsx import { defineIsolate, useHost } from '@isoloop/react'; import { useRef, useState } from 'react'; function Measured() { const host = useHost(); const box = useRef(null); const [width, setWidth] = useState(0); const measure = async () => { if (!box.current) return; const rect = await host.measure(box.current); setWidth(rect.width); }; return (
void measure()}> {width > 0 ? `${width}px wide` : 'Click to measure'}
); } export default defineIsolate(Measured); ``` A library that measures on every render (tooltips that position themselves, virtualised lists, auto-sizing text) needs rewriting around these calls, or it belongs on the page rather than in an isolate. ## Globals that libraries reach for In an isolate with a surface, `document` is the render bridge's DOM subset and `window` is the worker's own global. A headless isolate has neither. That breaks some libraries in ways that are easy to miss: - **`document.body`** is a detached element. A widget that appends a portal, a tooltip or a modal to it renders nothing on screen. Render it inside your own root instead. - **`localStorage` and `sessionStorage`** don't exist in workers. `IndexedDB` does. To keep state across a restart, publish it to a topic ([persistent topics](https://isoloop.dev/docs/hosting.md#persistent-topics-and-a-discarded-tab) survive a reload). - **The page's `window`** isn't there: no `innerWidth`, `matchMedia` or `history`, and `location` is the worker script's URL. Navigation goes through `host.navigate`, with the `navigate` grant ([contracts and grants](https://isoloop.dev/docs/contracts.md)). - **Tags the page won't create.** The page builds only allow-listed HTML and SVG tags: an unknown tag becomes a `div` (`g` inside SVG), and `script`, `iframe`, `style` and `on*` attributes are never created. URLs are limited to `http(s):`, `mailto:` and relative ones. A widget that injects a `