mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-03 23:03:09 +00:00
fix(studio): server-side DOM patching, render CSS scoping, and resilience
Root-cause fix for edits being wiped after refresh: the studio's
inspector edits were patched client-side via regex matching in
sourcePatcher.ts, which silently failed for many compositions ("Unable
to patch" toast). Replaced with a server-side patch-element API endpoint
using linkedom for proper DOM parsing via querySelector.
Also fixes the WYSIWYG render bug where sub-composition CSS was not
applied. The CSS scoping generated descendant selectors when both
attributes coexist on the same host element. Fixed to use compound
selectors for the authored root.
Edit persistence:
- New POST /file-mutations/patch-element endpoint using linkedom
- persistDomEditOperations calls server instead of client regex
- 15 tests covering all patch operation types
Render CSS scoping:
- Compound selector for authored root on host element
- Regression test: wysiwyg-subcomp-css (baseline pending Docker)
- 3 unit tests + 1 integration test
GSAP CDN fallback:
- Preview: error-handler catches gsap 404 and loads from CDN
- Producer: rewrites missing local gsap paths to CDN before compile
Studio resilience:
- Error boundary with recoverable UI
- Lazy mediabunny import prevents crash cascade
- Hash routing listens for hashchange events
- Sub-composition duration reads data-hf-authored-duration fallback
- Save debounce 600ms to requestAnimationFrame
Observability:
- PostHog telemetry for crashes, save failures, tab switches, playback,
toolbar actions, navigation, and render starts
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
const POSTHOG_API_KEY = "phc_zjjbX0PnWxERXrMHhkEJWj9A9BhGVLRReICgsfTMmpx";
|
||||
const POSTHOG_HOST = "https://us.i.posthog.com";
|
||||
const FLUSH_INTERVAL_MS = 30_000;
|
||||
const FLUSH_TIMEOUT_MS = 5_000;
|
||||
|
||||
interface EventProperties {
|
||||
[key: string]: string | number | boolean | null | undefined;
|
||||
}
|
||||
|
||||
interface QueuedEvent {
|
||||
event: string;
|
||||
properties: EventProperties;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
let queue: QueuedEvent[] = [];
|
||||
let flushTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let distinctId: string | null = null;
|
||||
|
||||
function getDistinctId(): string {
|
||||
if (distinctId) return distinctId;
|
||||
try {
|
||||
const stored = localStorage.getItem("hf-studio-anon-id");
|
||||
if (stored) {
|
||||
distinctId = stored;
|
||||
return stored;
|
||||
}
|
||||
} catch {
|
||||
// localStorage may be unavailable
|
||||
}
|
||||
distinctId = crypto.randomUUID();
|
||||
try {
|
||||
localStorage.setItem("hf-studio-anon-id", distinctId);
|
||||
} catch {
|
||||
// best-effort persistence
|
||||
}
|
||||
return distinctId;
|
||||
}
|
||||
|
||||
function isEnabled(): boolean {
|
||||
try {
|
||||
return localStorage.getItem("hf-studio-telemetry-opt-out") !== "1";
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function getSessionProperties(): EventProperties {
|
||||
return {
|
||||
studio_version: __STUDIO_VERSION__,
|
||||
screen_width: window.screen?.width,
|
||||
screen_height: window.screen?.height,
|
||||
viewport_width: window.innerWidth,
|
||||
viewport_height: window.innerHeight,
|
||||
user_agent: navigator.userAgent,
|
||||
url_hash: location.hash.replace(/#project\//, ""),
|
||||
};
|
||||
}
|
||||
|
||||
declare const __STUDIO_VERSION__: string;
|
||||
|
||||
export function trackStudioEvent(event: string, properties: EventProperties = {}): void {
|
||||
if (!isEnabled()) return;
|
||||
|
||||
queue.push({
|
||||
event: `studio:${event}`,
|
||||
properties: { ...getSessionProperties(), ...properties },
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
if (!flushTimer) {
|
||||
flushTimer = setInterval(flushEvents, FLUSH_INTERVAL_MS);
|
||||
}
|
||||
}
|
||||
|
||||
async function flushEvents(): Promise<void> {
|
||||
if (queue.length === 0) return;
|
||||
|
||||
const batch = queue.map((e) => ({
|
||||
event: e.event,
|
||||
properties: { ...e.properties, $ip: null },
|
||||
distinct_id: getDistinctId(),
|
||||
timestamp: e.timestamp,
|
||||
}));
|
||||
queue = [];
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), FLUSH_TIMEOUT_MS);
|
||||
|
||||
try {
|
||||
await fetch(`${POSTHOG_HOST}/batch/`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ api_key: POSTHOG_API_KEY, batch }),
|
||||
signal: controller.signal,
|
||||
});
|
||||
} catch {
|
||||
// Telemetry must never break the studio
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
// Flush on page unload so we don't lose the last batch
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("visibilitychange", () => {
|
||||
if (document.visibilityState === "hidden") {
|
||||
if (queue.length === 0) return;
|
||||
const batch = queue.map((e) => ({
|
||||
event: e.event,
|
||||
properties: { ...e.properties, $ip: null },
|
||||
distinct_id: getDistinctId(),
|
||||
timestamp: e.timestamp,
|
||||
}));
|
||||
queue = [];
|
||||
const body = JSON.stringify({ api_key: POSTHOG_API_KEY, batch });
|
||||
try {
|
||||
navigator.sendBeacon(`${POSTHOG_HOST}/batch/`, body);
|
||||
} catch {
|
||||
// best-effort
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user