Skip to content

Commit 4682da1

Browse files
committed
fix(sdk): attachSync re-syncs the override snapshot on iframe load
1 parent db5e062 commit 4682da1

3 files changed

Lines changed: 67 additions & 9 deletions

File tree

‎docs/sdk/reference/adapters.mdx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ interface PreviewAdapter {
121121
</ParamField>
122122

123123
<ParamField path="attachSync" type="(comp: Composition) => () => void">
124-
Mirrors a composition's edits onto the adapter's own live document: an immediate full sync of the composition's current overrides, then a subscription that replays every future `patch` event — including undo/redo, since both fire through the same event with forward or inverse patches. Calling `attachSync` again while already attached detaches the previous subscription first. Returns an unsubscribe function.
124+
Mirrors a composition's edits onto the adapter's own live document: an immediate full sync of the composition's current overrides, then a subscription that replays every future `patch` event — including undo/redo, since both fire through the same event with forward or inverse patches. Calling `attachSync` again while already attached detaches the previous subscription first. The full-override sync also re-runs on every iframe `load`, so a `srcdoc` navigation that races the attach (or drops patches committed during the load window) converges once the new document arrives. Returns an unsubscribe function.
125125

126126
Script-tag patches (`/script/gsap` and any future `/script/*` path) are never mirrored — rewriting a live `<script>` tag's content doesn't re-execute it, and re-running GSAP setup from scratch would conflict with running timeline state. Every other patch kind (style, text, attribute, timing, hold, element add/remove, stylesheet, variable value, variable declaration) mirrors as-is.
127127

@@ -335,4 +335,3 @@ preview.commitPreview();
335335
Building a visual editor canvas with the iframe preview adapter and hit-testing.
336336
</Card>
337337
</CardGroup>
338-

‎packages/sdk/src/adapters/iframe.sync.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,51 @@ window.__timelines = { t: tl };</script>
252252
expect(liveTitle.getAttribute("data-end")).toBe("3");
253253
});
254254
});
255+
256+
describe("IframePreviewAdapter.attachSync — iframe load re-sync", () => {
257+
it("re-applies the override snapshot when the iframe fires `load`", async () => {
258+
const iframe = mountIframe(BASE_HTML);
259+
const comp = await openComposition(BASE_HTML);
260+
const adapter = createIframePreviewAdapter(iframe);
261+
adapter.attachSync(comp);
262+
comp.setStyle("hf-title", { color: "#f00" });
263+
264+
// Simulate a navigation: replace the document with a fresh base (the
265+
// mirrored edit is gone), then fire the load event a real srcdoc
266+
// assignment would produce.
267+
const midDoc = iframe.contentDocument;
268+
if (!midDoc) throw new Error("iframe has no document");
269+
midDoc.open();
270+
midDoc.write(BASE_HTML);
271+
midDoc.close();
272+
iframe.dispatchEvent(new Event("load"));
273+
274+
const liveTitle = iframe.contentDocument?.querySelector(
275+
'[data-hf-id="hf-title"]',
276+
) as HTMLElement;
277+
expect(liveTitle.style.getPropertyValue("color")).toBe("#f00");
278+
});
279+
280+
it("stops re-syncing on load after detach", async () => {
281+
const iframe = mountIframe(BASE_HTML);
282+
const comp = await openComposition(BASE_HTML);
283+
const adapter = createIframePreviewAdapter(iframe);
284+
const detach = adapter.attachSync(comp);
285+
comp.setStyle("hf-title", { color: "#f00" });
286+
detach();
287+
288+
const midDoc = iframe.contentDocument;
289+
if (!midDoc) throw new Error("iframe has no document");
290+
midDoc.open();
291+
midDoc.write(BASE_HTML);
292+
midDoc.close();
293+
iframe.dispatchEvent(new Event("load"));
294+
295+
const liveTitle = iframe.contentDocument?.querySelector(
296+
'[data-hf-id="hf-title"]',
297+
) as HTMLElement;
298+
// BASE_HTML's authored inline color remains because detach must not
299+
// re-apply comp's #f00 override on the load event.
300+
expect(liveTitle.style.getPropertyValue("color")).toBe("#fff");
301+
});
302+
});

‎packages/sdk/src/adapters/iframe.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -752,17 +752,27 @@ class IframePreviewAdapter implements PreviewAdapter {
752752
attachSync(comp: Composition): () => void {
753753
this._syncDetach?.();
754754

755-
const doc = this.iframe.contentDocument;
756-
if (doc) {
755+
const syncOverrides = (): void => {
756+
const doc = this.iframe.contentDocument;
757+
if (!doc) return;
757758
try {
758759
applyOverrideSet({ document: doc, wrapped: false, stamped: "" }, comp.getOverrides());
759760
} catch (err) {
760-
// Don't let a bad initial snapshot prevent the ongoing subscription
761-
// below from attaching — future patches should still mirror even if
762-
// this composition's current overrides couldn't be applied.
763-
console.warn("[hyperframes] attachSync: initial override sync failed:", err);
761+
// Don't let a bad snapshot prevent the ongoing subscription below
762+
// from attaching — future patches should still mirror even if this
763+
// composition's current overrides couldn't be applied.
764+
console.warn("[hyperframes] attachSync: override sync failed:", err);
764765
}
765-
}
766+
};
767+
768+
// Immediate snapshot for the current document…
769+
syncOverrides();
770+
// …and again on every iframe `load`: assigning srcdoc/src is an ASYNC
771+
// navigation, so an attach in the same tick snapshots the OUTGOING
772+
// document, and patches committed during the load window mirror into it
773+
// and die with it. Re-syncing on load converges the new document with
774+
// the composition state regardless of attach/navigation ordering.
775+
this.iframe.addEventListener("load", syncOverrides);
766776

767777
const rawUnsubscribe = comp.on("patch", ({ patches }) => {
768778
const liveDoc = this.iframe.contentDocument;
@@ -778,6 +788,7 @@ class IframePreviewAdapter implements PreviewAdapter {
778788
});
779789

780790
const detach = (): void => {
791+
this.iframe.removeEventListener("load", syncOverrides);
781792
rawUnsubscribe();
782793
if (this._syncDetach === detach) this._syncDetach = null;
783794
};

0 commit comments

Comments
 (0)