Browse documentation
Fountain handbook

Browser mount contract

Write the browser module behind a page or panel: export mount(root, context), call your backend through the scoped helpers, navigate the forge and follow the host's lifecycle.

On this page

The entry module

Each page and panel entry is an ES module under assets/ that exports mount:

export async function mount(root, context) {
  if (context.contractVersion !== 1) throw new TypeError('Unsupported browser contract version');
  const controller = new AbortController();
  const output = document.createElement('pre');
  output.textContent = 'Loading…';
  root.append(output);
  try {
    const response = await context.request('context', {signal: controller.signal});
    if (!response.ok) throw new Error(`Request failed: ${response.status}`);
    output.textContent = JSON.stringify(await response.json(), null, 2);
  } catch (error) {
    if (!controller.signal.aborted) output.textContent = 'Could not load context.';
  }
  return () => controller.abort();
}

The host calls mount once per root. It may be asynchronous. Check contractVersion before doing anything else and refuse a version your build does not support. Render only inside root, treat every value from the server as text, and release listeners, timers and requests when the host tears the mount down.

The canonical TypeScript definition is sdk/browser/extension-contract.d.ts in the Fountain source, for the same release as your target forge. It is type-only: the host supplies the helpers at runtime, so your shipped JavaScript imports nothing from the SDK. To typecheck JavaScript, reference the types in JSDoc, as the bundled example does.

The mount context

Member Type Meaning
extensionId string Your package ID
pageId string, optional The page ID, when mounted as a page
panelId string, optional The panel ID, when mounted as a panel
ownerId string, optional The shared owner, when a page and panel share one instance
apiBase string Scoped API prefix ending in /api/; every request stays under it
assetBase string Prefix your package's assets are served from
appSubUrl string The forge's sub-path, empty at the root
sessionGeneration string The native session generation the mount was admitted under
contractVersion 1 The browser contract version the host implements
repositoryId string, optional The repository being viewed, for display only
request function Scoped fetch to your backend
stream function Scoped WebSocket to your backend
navigate function Ask the host to move to another forge address

repositoryId and the presentation facts described below are display information. They never grant authority: the backend receives the authoritative actor and repository from the host.

Call your backend

request(relativePath, options) sends a request to apiBase + relativePath and returns a Response. Options accept method, headers, body and signal. The helper attaches the session generation and refuses cross-origin, credential-bearing or encoded-separator targets without sending them. In-flight requests abort when the mount retires.

const response = await context.request('notes', {
  method: 'PUT',
  headers: {'Content-Type': 'text/plain; charset=utf-8'},
  body: text,
});

The host forwards GET, HEAD, POST, PUT, PATCH and DELETE. A 409 response with Page session changed means the browser session changed since the mount was admitted, and the request was not forwarded. See Extension backend for size and time limits.

Open a stream

stream(relativePath, options) opens a WebSocket to your backend through the host and returns an object with the underlying socket and a close(code, reason) method. The only option is signal. No query string, subprotocols or extra headers are sent; the host admits the stream from the live session.

const live = context.stream('stream');
live.socket.addEventListener('message', (event) => {
  status.textContent = event.data;
});

The socket stays open across updates, suspension and resumption. The host closes it when the mount retires or access is revoked.

Call navigate(path, options) to move to another forge address instead of assigning window.location. It adds a history entry; pass {replace: true} to replace the current one, for example after saving a draft.

The host decides how to handle the destination. Supported forge addresses, including your declared subroutes, change in place and keep retained panels mounted. Sign-in, credential-bearing and cross-origin destinations load as full documents, and private host prefixes are refused. Navigation is routing only and grants no authority. After the mount retires, navigate throws.

Lifecycle

mount may return nothing, a cleanup function, an object with dispose(), or a lifecycle handle. Every callback is driven by the host.

Callback When the host calls it What to do
update(presentation) Visibility, mode, route or observed repository changed Update what you display. Root, resources and streams stay connected.
suspend() The instance is hidden while navigating, or the host is revalidating the session Hide private state and pause visible work. Keep resources and streams.
resume(validation) After suspension, once the session is confirmed Resume visible work. validation.sessionGeneration carries the confirmed generation.
retire(reason) Final teardown Release everything. The host has already closed your streams.
dispose() Teardown, at most once Release listeners, timers and requests.

presentation contains mode (page or panel), the current route, decoded route params, an optional repositoryId and visible.

reason is one of session-changed, authority-revoked or contribution-unavailable. Ordinary navigation never retires a retained instance: the visible instance receives update, and a hidden one is suspended and resumed. A full page reload creates a fresh mount.

export function mount(root, context) {
  const clock = document.createElement('p');
  root.append(clock);
  const tick = () => { clock.textContent = new Date().toLocaleTimeString(); };
  const timer = window.setInterval(tick, 1000);
  return {
    update(presentation) {
      root.hidden = !presentation.visible;
    },
    suspend() {
      root.hidden = true;
    },
    resume() {
      root.hidden = false;
    },
    retire() {
      window.clearInterval(timer);
    },
    dispose() {
      window.clearInterval(timer);
    },
  };
}

Styling and assets

Your module owns layout and styling inside its root. The forge's theme and native components belong to the host; do not resize or restyle the page body, navigation bar or footer. Load additional modules and files from your package with paths relative to the entry module, or from assetBase. Assets are served only to signed-in users, without caching.

Continue

Implement the endpoints your module calls in the Extension backend.