v156 · dom · feature removal · migration

Explicit teardown handshake

An embedded same-origin draft editor saved its unsent text in pagehide, because that used to fire when the host page closed the panel with iframe.remove(). With the events omitted, that save never runs. Here is the real widget and three ways to close it — the old reliance, and two replacements that work in every browser, flag or no flag.

Flag: to watch strategy A lose the draft, run Chrome 156+ with --enable-blink-features=OmitSubframeDetachmentEventsOnRemoval. Strategies B and C do not depend on the flag at all — that’s the point.

Close the widget three ways

No widget open. Opening one is automatic when you pick a strategy; open it first if you want to type in it.

Pick a strategy.

Drafts that reached the host’s store
strategydraft saved?saved bycharacters
Nothing closed yet.

the replacement patterns

// B · explicit teardown: ask, wait (bounded), then remove
async function closeWidget(frame, port) {
  const ack = new Promise(r => port.addEventListener("message",
    e => e.data.type === "saved" && r(e.data), { once: true }));
  port.start();
  port.postMessage("teardown");
  const saved = await Promise.race([ack, timeout(500)]);  // never block the UI forever
  if (saved) store(saved.draft);
  frame.remove();
}

// C · the parent owns the state: mirror it continuously, persist on removal
port.onmessage = e => { if (e.data.type === "state") latest.set(frame, e.data.draft); };
new MutationObserver(records => {
  for (const r of records) for (const n of r.removedNodes)
    if (latest.has(n)) store(latest.get(n));       // runs after removal, as a microtask
}).observe(host, { childList: true });

// Inside the widget: flush is idempotent, so the legacy pagehide path can stay as a fallback
let flushed = false;
function flush(via) { if (flushed) return; flushed = true; /* … */ }
addEventListener("pagehide", () => flush("pagehide"));   // still right for navigations

For analytics or telemetry rather than user data, the guidance for whole-session beacons is fetchLater(), scheduled as data arrives, rather than sending from an unload-time handler — it is delivered when the document is destroyed, whatever caused that.

see also