Port patterns / Off the main thread: Actors, Remote DOM and media
Untrusted plugin UI without an iframe (Remote DOM over a port)
Embedding third-party extensions in an <iframe> gives you security, but you get an awkward rectangular box that fights your theme, layout and keyboard focus. Shopify's remote-dom (for Checkout UI Extensions), Figma's dual-context plugins and Paul Kinlan's extensible web app experiments take another route: run the plugin headless inside a Web Worker with zero DOM, zero window and zero cookies, and let it stream semantic UI descriptions (Badge, Button, Meter) down a MessagePort. The host renders native, theme-matched elements through a strict allowlist.
When you'd use this
An e-commerce checkout or document editor lets third-party developers add loyalty badges, promo buttons and status meters right inside the host layout. Running each extension headless in a worker and rendering only an allowlisted component tree over a MessagePort keeps your brand typography, focus order and dark mode intact while blocking untrusted scripts from touching payment inputs or cookies.
Code
// plugin worker: zero DOM, streams semantic UI descriptions
port.postMessage({
type: "tree",
nodes: [
{ type: "Badge", text: "3 words" },
{ type: "Button", id: "shout", label: "UPPERCASE" },
],
});
// host page: strict allowlist renderer — native CSS, zero innerHTML
const ALLOWED = new Set(["Badge", "Button", "Meter"]);
function mountNode(node, port) {
if (!ALLOWED.has(node.type)) return null; // blocks <script>, <img>, <iframe>
if (node.type === "Button") {
const btn = document.createElement("button");
btn.textContent = String(node.label); // onclick & unknown props ignored
btn.onclick = () => port.postMessage({ event: "click", id: node.id });
return btn;
}
}
In practice
// host checkout page: mount an untrusted discount plugin in a worker
const plugin = new Worker("/sandbox/discount-plugin.js");
const { port1, port2 } = new MessageChannel();
plugin.postMessage({ cmd: "init", cartTotal: 4200 }, [port2]);
const COMPONENTS = {
Badge: (props) => {
const el = document.createElement("span");
el.className = "checkout-badge";
el.textContent = String(props.text || "");
return el;
},
Button: (props) => {
const el = document.createElement("button");
el.className = "checkout-btn";
el.textContent = String(props.label || "");
el.onclick = () => port1.postMessage({ event: "click", id: String(props.id) });
return el;
},
};
port1.onmessage = ({ data }) => {
const nodes = (data.nodes || [])
.filter((n) => Object.hasOwn(COMPONENTS, n.type))
.map((n) => COMPONENTS[n.type](n));
extensionSlot.replaceChildren(...nodes);
};
How it works
Giving third-party plugins their own UI on the web has always had a painful trade-off: run them in your page and you've handed over your cookies, DOM and user session to XSS; put them in a sandboxed <iframe> and you're stuck with a rigid box that can't match your typography, dark mode, popovers or layout flow.
Remote DOM — used in production by Shopify's Checkout UI Extensions (@remote-dom/core), Figma's dual-context plugins and explored in Paul Kinlan's extensible web apps work — splits execution from rendering. The untrusted plugin runs headless in a Web Worker (or a sandboxed cross-origin iframe with no visible box), where window, document and cookies don't exist.
Instead of touching the DOM directly, the plugin streams a minimal, serialised tree of semantic components (Badge, Button, Meter) down a MessagePort. The host app owns the real DOM and maps each allowed component to native, theme-matched elements, wiring click and input events back over the same port.
Because the host runs a strict component and prop allowlist — and never touches innerHTML — a hostile plugin that tries to send a <script> tag or an inline onclick handler gets silently rejected before anything hits the document.
Step by step
What each press of Next does in the demo above.
- Give both Worker plugins UI ports. The host spawns two headless Web Workers — neither has window, document or cookies — and transfers a UI MessagePort to each.
- Text Tools Plugin mounts native UI. The Text Tools worker posts a semantic tree of Badge and Button descriptors. The host mounts real, theme-matched HTML controls directly inside its own slot.
- Click the plugin's native button. Clicking the rendered UPPERCASE button sends a click event back down the port. The worker transforms the text and streams back an updated UI tree.
- Hostile Plugin tries script tag and onclick. The hostile worker sends a script node and a Button carrying an inline onclick attribute. The host allowlist drops the script tag and strips onclick — zero XSS.
What you can build with it
Other ideas
- Checkout, CMS and dashboard extension slots where third-party partners need native-looking controls that inherit your design system and dark mode.
- Editor sidebars and tool palettes (like Figma or text-tool plugins) where community scripts shouldn't be able to read user tokens or scrape unrelated page content.
- AI-generated or agent-supplied micro-UIs where you want rich interactive cards rendered safely from a bounded component vocabulary.
Things to try
- Mount the Text Tools Plugin and inspect the Host App box: the Badge and UPPERCASE button use this site's native CSS without an iframe anywhere.
- Click the rendered UPPERCASE button directly inside the Host App box (or press Next) to see the click event round-trip to the Worker and update the host's document.
- Trigger the Hostile Plugin and check the log: the script tag is dropped and the onclick attribute is stripped before mounting.
Gotchas
- A same-origin Blob Worker still runs under your origin for
fetch()and IndexedDB unless you restrict it with CSP or host plugin code on a separate sandboxed origin. For genuinely hostile third-party code, pair Remote DOM with a sandboxed cross-origin worker/iframe boundary or strict CSP (connect-src 'none'). - Don't sneak raw HTML strings, arbitrary
styleobjects or unvalidatedhrefURLs (likejavascript:) through your allowlist — every prop you allow is part of your attack surface. - Remote DOM is asynchronous across a port, so it's a poor fit for 60fps drag handles, synchronous layout reads (
getBoundingClientRect) or rich text editors. Reserve it for declarative UI trees and discrete user events. - Keep the component protocol small and versioned. Re-implementing the entire HTML DOM over
postMessagerecreates most of the complexity you were trying to avoid.
References
The web platform features this page mentions, with what each one is, its documentation on MDN and the specification that defines it.
- <iframe>
- An element that shows another page inside this one. The inner page can be on a different origin, and the two can only talk by posting messages. MDNHTML Standard
- Web Workers
- Scripts that run on their own thread, away from the page. MDNHTML Standard
- MessagePort
- One end of a message channel. Whatever is posted into it comes out of the other end, and it can be sent to another page, frame or worker. MDNHTML Standard
- Blob
- A fixed piece of binary data, such as the contents of a file. It is copied when posted, and the copy points at the same bytes. MDNFile API
- IndexedDB
- A database in the browser that belongs to one origin. It can store most things that can be posted, including a CryptoKey. MDNIndexed Database API
- Content Security Policy
- A header that limits what a page may load and where it may connect. MDNContent Security Policy
- MessagePort.postMessage()
- Sends a message through a port. A second argument lists the objects to move instead of copy. MDNHTML Standard
For more, see Further reading: articles, talks, papers, libraries and the specifications, collected in one place.