From 9797faea05944a260547d86a5a5b6a0ed89ad982 Mon Sep 17 00:00:00 2001 From: Santhi Prakash <38608178+santhiprakash@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:13:03 +0000 Subject: [PATCH] fix(core): drop motion-blur state on __hfRuntimeTeardown (#7996) The blur's filter is appended to , outside the body swap a same-document re-init performs. A torn-down run left it (and window.__hfMotionBlur) behind, so the next run's hf-mb-0 collided with the stale id and url(#hf-mb-N) drew movers through the old filter region, rendering them blank. Fixes #7986 --- .../src/runtime/captureMotionBlur/index.ts | 15 +++- packages/core/src/runtime/init.test.ts | 87 +++++++++++++++++++ packages/core/src/runtime/init.ts | 6 ++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/packages/core/src/runtime/captureMotionBlur/index.ts b/packages/core/src/runtime/captureMotionBlur/index.ts index 4d28a346d8..519c3d615e 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.ts @@ -826,16 +826,27 @@ function suspendCaptureMotionBlur(): void { instance?.suspend(); } -/** Test-only: drop module state between cases. */ -export function resetCaptureMotionBlurForTests(): void { +export function teardownCaptureMotionBlur(): void { instance?.teardown(); instance = null; activeHost = null; activeConfig = null; onFallback = false; suspended = false; + const w = window as CompositeWindow; + delete w.__hfMotionBlur; + if (ourResolver && w.__hf_page_composite_resolve === ourResolver) { + if (priorResolver) w.__hf_page_composite_resolve = priorResolver; + else delete w.__hf_page_composite_resolve; + w.__hf_page_composite_pending = false; + } priorResolver = null; ourResolver = null; +} + +/** Test-only: drop module state between cases. */ +export function resetCaptureMotionBlurForTests(): void { + teardownCaptureMotionBlur(); setFilterPreviewQuality(1); } diff --git a/packages/core/src/runtime/init.test.ts b/packages/core/src/runtime/init.test.ts index 3cbae1edd4..ba3fb47559 100644 --- a/packages/core/src/runtime/init.test.ts +++ b/packages/core/src/runtime/init.test.ts @@ -4654,6 +4654,93 @@ describe("initSandboxRuntimeModular", () => { expect(window.__renderReady).toBe(true); }); + it("drops motion-blur filters and its window api on teardown", () => { + const raf = createManualRaf(); + vi.spyOn(performance, "now").mockImplementation(() => raf.now()); + window.requestAnimationFrame = raf.requestAnimationFrame as typeof window.requestAnimationFrame; + window.cancelAnimationFrame = raf.cancelAnimationFrame as typeof window.cancelAnimationFrame; + + document.body.innerHTML = + `
` + + `
` + + `
`; + const slider = document.getElementById("slider")!; + for (const [k, v] of Object.entries({ + offsetWidth: 10, + offsetHeight: 10, + offsetLeft: 0, + offsetTop: 0, + offsetParent: document.getElementById("root")!, + })) { + Object.defineProperty(slider, k, { value: v, configurable: true }); + } + const x = () => Number(/translate3d\(([-\d.e]+)px/.exec(slider.style.transform)?.[1] ?? 0); + slider.getBoundingClientRect = () => + ({ left: x(), top: 0, right: x() + 10, bottom: 10, width: 10, height: 10 }) as DOMRect; + // A browser resolves the tween's translate to matrix(...) in the computed style; + // jsdom keeps the string. The pose reader only parses matrix(...). + const realComputedStyle = window.getComputedStyle.bind(window); + vi.spyOn(window, "getComputedStyle").mockImplementation((el, pseudoElt) => { + const cs = realComputedStyle(el, pseudoElt); + if (el !== slider) return cs; + return new Proxy(cs, { + get: (target, prop) => { + if (prop === "transform") return `matrix(1, 0, 0, 1, ${x()}, 0)`; + if (prop === "translate" || prop === "rotate" || prop === "scale") { + return Reflect.get(target, prop) || "none"; + } + const v = Reflect.get(target, prop); + return typeof v === "function" ? v.bind(target) : v; + }, + // gsap's transform parse writes the individual properties back to "none" + // on the computed view it was handed; jsdom has no setters for them. + set: () => true, + }); + }); + const tl = gsap.timeline({ paused: true }); + tl.to(slider, { x: 300, duration: 1, ease: "none" }, 0); + window.__timelines = { main: tl as unknown as RuntimeTimelineLike }; + + const blurWindow = window as { __hfMotionBlur?: unknown }; + const blurFilters = () => document.documentElement.querySelectorAll('filter[id^="hf-mb"]'); + + const rangeRect = Range.prototype.getBoundingClientRect; + Range.prototype.getBoundingClientRect = () => new DOMRect(); + try { + initSandboxRuntimeModular(); + window.__player?.play(); + for (let frame = 0; frame < 30; frame++) raf.step(16); + + expect(blurFilters()).toHaveLength(1); + expect(slider.style.filter).toMatch(/hf-mb-0/); + expect(blurWindow.__hfMotionBlur).toBeDefined(); + + window.__hfRuntimeTeardown?.(); + + // The filter sits on , outside the body swap a re-init performs. + // Left behind, the next run's hf-mb-0 collides with it and `url(#hf-mb-0)` + // resolves to the stale filter's old region, drawing the new mover blank. + expect(blurFilters()).toHaveLength(0); + expect(document.documentElement.querySelector("svg")).toBeNull(); + expect(blurWindow.__hfMotionBlur).toBeUndefined(); + expect(slider.style.filter).not.toMatch(/hf-mb/); + + window.__timelines = { main: tl as unknown as RuntimeTimelineLike }; + initSandboxRuntimeModular(); + window.__player?.play(); + for (let frame = 0; frame < 30; frame++) raf.step(16); + + const ids = [...blurFilters()].map((filter) => filter.id); + expect(document.documentElement.querySelectorAll("svg")).toHaveLength(1); + expect(ids).toEqual(["hf-mb-0"]); + expect(slider.style.filter).toMatch(/hf-mb-0/); + } finally { + Range.prototype.getBoundingClientRect = rangeRect; + } + }); + it("a torn-down runtime's pending readiness check leaves the next document alone", () => { vi.useFakeTimers(); try { diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index 32fe05fdff..7661dc570f 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -194,6 +194,7 @@ import { paintCaptureMotionBlur, releaseCaptureMotionBlurFrame, setMotionBlurPreviewQuality, + teardownCaptureMotionBlur, } from "./captureMotionBlur"; import { TransportClock } from "./clock"; import { WebAudioTransport } from "./webAudioTransport"; @@ -6819,6 +6820,11 @@ export function initSandboxRuntimeModular(): void { state.beforeUnloadHandler = null; } picker.disablePickMode(); + try { + teardownCaptureMotionBlur(); + } catch (err) { + swallow("runtime.init.site17", err); + } for (const adapter of state.deterministicAdapters) { if (!adapter || typeof adapter.revert !== "function") continue; try {