# iframes

Each team's app is a separate page shown in a frame. Nothing leaks in or out, and nothing is shared, including the cost.

## How it works

1. **A frame per app.** The shell renders an iframe for each team's page.
2. **Separate document.** Each frame loads its own HTML, JavaScript and CSS from the team's server, as its own browsing context.
3. **Again for team B.** Team B's frame does the same, with its own document and resources.
4. **postMessage.** Frames talk to the shell with postMessage, naming the exact target origin and checking the sender's origin.

## Try it

**Demo: Style isolation.**

Team A ships .button { background: orange; border-radius: 0 } and body { font-family: Georgia, serif }, and Team B sets no font of its own. Without isolation, both rules are global: Team B's Pay now button turns orange and its text changes font. With CSS Modules, the class is renamed to something like .button_x7f2a, so Team B's button is safe, but body is a tag selector and stays global, so the font still changes. With Shadow DOM, selectors can't cross the shadow root, so the button is safe, but inherited properties such as the font still pass from the host into the shadow tree unless Team B resets them. With an iframe, Team B runs in its own document: neither rule reaches it, and nothing is shared either, at the cost of a whole extra page load.

Takeaway: Isolation is a spectrum, not a switch. CSS Modules scope class names but not global selectors. Shadow DOM blocks selectors, yet inherited properties like fonts still flow in. Only an iframe isolates everything, and it costs a whole page load.

## Pros

- Strongest isolation: styles, globals and uncaught errors stay inside the frame.
- Each frame can use any technology and version.
- The simplest way to embed an app you cannot change.

## Cons

- Every frame costs memory and loads its own resources.
- Routing, history, deep links and resizing have to be synced by hand.
- Each frame needs a title, and moving between frames is harder for screen reader users.

## Best when

- Embedding third-party or untrusted content, together with the sandbox attribute.
- Legacy apps you cannot change.
- Internal tools where load time matters less.

## Avoid when

- Public, SEO-critical pages.
- Tightly integrated interfaces that share state.
- Tight mobile performance budgets.

## In code

`shell.html · team-b.example.com/cart (inside the frame)`

```html
<!-- shell.html -->
<iframe src="https://team-b.example.com/cart"
        title="Cart" loading="lazy"></iframe>

<script>
  // Accept messages only from team B's exact origin
  window.addEventListener("message", (event) => {
    if (event.origin !== "https://team-b.example.com") return;
    if (event.data?.type === "cart:updated") updateCartBadge(event.data.count);
  });
</script>

<!-- team-b.example.com/cart, inside the frame -->
<script>
  // Name the exact target origin, never "*"
  window.parent.postMessage(
    { type: "cart:updated", count: 2 },
    "https://shop.example.com"
  );
</script>
```

## Ratings

_Editorial judgement for a typical team, from 1 to 5._

- **Team independence: 5/5** (higher is better). Every frame is a separate app, served and deployed by its own team.
- **Runtime performance: 1/5** (higher is better). Each frame loads its own document and resources and needs extra memory.
- **Complexity: 2/5** (lower is simpler). Embedding is easy; sizing, routing and messaging between frames are the hard parts.
- **SSR / SEO support: 2/5** (higher is better). A frame's page can be server-rendered, but search engines index it separately from the page that embeds it, so the embedding page doesn't get credit for that content.
- **Tech-agnostic: 5/5** (higher is better). Anything that can render a web page can be framed.
- **Isolation: 5/5** (higher is better). Each frame has its own document, so styles and globals don't interfere, and sandbox can restrict it further. A same-site frame can still share the page's main thread, so a frame that hangs can freeze the page.
- **UX consistency: 1/5** (higher is better). Integrating parts across frames is difficult, and responsive layout and deep links get harder.
- **Tooling maturity: 5/5** (higher is better). Built into every browser, with no framework or build tool needed.

## Tooling and ecosystem

iframes are old, but that is also why they are well proven. Natalia Venditto still counts them as a valid choice when encapsulation is the priority, because a frame comes with a full sandbox out of the box, and notes they can be combined with other techniques such as web components.

There is nothing to install: `<iframe>` and `postMessage` are standard browser features documented on MDN. Two details matter in practice. Give every frame a `title` that describes its content, so screen reader users know what it holds without entering it. When frames send messages, always pass an exact target origin instead of `*`, and check `event.origin` in the receiving window. For untrusted content, the `sandbox` attribute restricts what a frame can do; MDN warns that combining `allow-scripts` and `allow-same-origin` for same-origin content removes that protection.

## Sources

- [<iframe>: The Inline Frame element](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/iframe), MDN Web Docs
- [Window: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage), MDN Web Docs
- [Micro Frontends](https://martinfowler.com/articles/micro-frontends.html), Cam Jackson · martinfowler.com
- [Iframes and micro-frontends](https://microfrontend.dev/architecture/iframes-and-microfrontends/), Natalia Venditto · microfrontend.dev

https://microfrontends.dev/approaches/iframes