Skip to content

Run in a worker

Importing Terminal from ghostty-webgpu/worker moves escape parsing, native screen state, Zig frame building, and WebGPU or WebGL2 rendering off the main thread onto an OffscreenCanvas. The page keeps only a lightweight DOM host for focus, keyboard/pointer events, layout, and accessibility.

You will learn

  • When worker execution helps (and when the main entry is simpler and cheaper)
  • How to mount a worker terminal and supply its required font faces
  • How async worker methods differ from the synchronous main entry
  • How to share workers, GPU devices, and glyph atlases with TerminalWorkerPool

Stay on the main entry (ghostty-webgpu) when you mount one or a few terminals and want the lowest total whole-browser CPU plus synchronous write() and readLines().

Switch to ghostty-webgpu/worker when:

  • Your UI must stay smooth while multiple terminals stream heavy output simultaneously.
  • You render many panes at once (for example 8 to 16 terminals) where main-thread parsing would blow past a 16.6 ms frame budget.
  • You want a producer worker to stream PTY bytes directly into the terminal via attachOutputPort(port) without touching the page thread.
worker.ts
import { Terminal } from 'ghostty-webgpu/worker'
export async function mountWorker(host: HTMLElement, fontUrl: string) {
const terminal = await Terminal.create({
backend: 'auto',
fonts: [{ family: 'Terminal Mono', source: { url: fontUrl } }],
appearance: { font: { family: 'Terminal Mono', size: 14 } },
})
await terminal.open(host)
await terminal.writeln('The terminal core and GPU renderer run in a worker.')
terminal.onData(({ bytes }) => {
void terminal.write(bytes)
})
terminal.focus()
return () => terminal.dispose()
}

Workers do not inherit fonts loaded into the page’s document.fonts. Pass every font face the worker needs through fonts as an absolute URL or raw Uint8Array bytes so the worker can register them in its own FontFaceSet, measure exact cell geometry, and rasterize glyphs.

By default, the worker resolves ghostty-vt.wasm, bridge.wasm, and the worker bundle relative to the package module. Pass assets: { wasm, bridge } and workerUrl when serving from a custom CDN or non-root base path.

Check three Content Security Policy requirements:

  • worker-src must allow workerUrl.
  • script-src must permit WebAssembly compilation ('wasm-unsafe-eval').
  • Cross-origin font and WASM URLs need matching CORS headers.

Worker mode communicates over MessagePort and transferable buffers, so SharedArrayBuffer and cross-origin isolation (COOP / COEP) are not required.

Because libghostty-vt lives inside the worker, every call that reads or mutates native state returns a promise: write, writeln, readLines, lineCount, geometry, setAppearance, setFont, setTheme, getSelection, and dispose. Await a write before calling a read that depends on it. Pure DOM methods like focus() and blur() remain synchronous.

Worker terminals support backend: 'auto' | 'webgpu' | 'webgl', registerLinkProvider, and focusNextLink. Main-thread-only capabilities (connectInput, ghostty-webgpu/hotkeys, canvas2d-*, and presentation: 'immediate') reject with a TerminalWorkerError whose code, why, and fix properties explain what failed.

Spawning one dedicated worker per terminal duplicates WASM instances, GPU devices, and glyph atlases. Pass a shared TerminalWorkerPool when mounting several terminals on one page:

worker-pool.ts
import { Terminal, TerminalWorkerPool } from 'ghostty-webgpu/worker'
// One pool for the page; its terminals share a few workers instead of one worker each.
const pool = new TerminalWorkerPool()
export async function mountPooledWorker(host: HTMLElement, fontUrl: string) {
const terminal = await Terminal.create({
pool,
backend: 'auto',
fonts: [{ family: 'Terminal Mono', source: { url: fontUrl } }],
appearance: { font: { family: 'Terminal Mono', size: 14 } },
})
await terminal.open(host)
return () => terminal.dispose()
}
How TerminalWorkerPool schedules and isolates terminals
  • Spawns one worker per terminal up to workers (default: min(4, Math.floor(navigator.hardwareConcurrency / 2))), then places additional terminals on the least-loaded worker.

  • Shares a worker only among terminals with identical workerUrl and fonts.

  • Terminals on the same worker share its GPU device, glyph atlas, and font faces, and shut the worker down once the last terminal disposes.

  • An error in one terminal closes only that terminal; an unrecoverable worker crash fails every terminal leased on that worker.

Recap

  • Always pass explicit fonts (URLs or bytes) to ghostty-webgpu/worker because workers cannot read document.fonts.

  • Await native operations on the worker entry and watch for TerminalWorkerError on main-thread-only capabilities.

  • Share a single TerminalWorkerPool across multi-pane layouts to pool GPU devices and glyph atlases.