Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio): SSR-safe player load, captions import cleanup (#248)
Miguel Ángelbf0d69885843
diff --git a/packages/player/package.json b/packages/player/package.jsonindex 0f586460a5..bd0ed436bc 100644--- a/packages/player/package.json+++ b/packages/player/package.json@@ -1,6 +1,6 @@{"name": "@hyperframes/player",- "version": "0.2.6",+ "version": "0.2.7","description": "Embeddable web component for HyperFrames compositions","repository": {"type": "git",diff --git a/packages/studio/package.json b/packages/studio/package.jsonindex fb0f635849..3999ff7200 100644--- a/packages/studio/package.json+++ b/packages/studio/package.json@@ -1,6 +1,6 @@{"name": "@hyperframes/studio",- "version": "0.2.5",+ "version": "0.2.9","description": "","repository": {"type": "git",diff --git a/packages/studio/src/captions/generator.test.ts b/packages/studio/src/captions/generator.test.tsindex 94c4bce475..dabcec73b1 100644--- a/packages/studio/src/captions/generator.test.ts+++ b/packages/studio/src/captions/generator.test.ts@@ -1,7 +1,7 @@// @vitest-environment nodeimport { describe, it, expect } from "vitest";-import { generateCaptionHtml } from "./generator.js";-import { buildCaptionModel, TranscriptWord } from "./parser.js";+import { generateCaptionHtml } from "./generator";+import { buildCaptionModel, TranscriptWord } from "./parser";// ---------------------------------------------------------------------------// Fixturesdiff --git a/packages/studio/src/captions/generator.ts b/packages/studio/src/captions/generator.tsindex 06fff5742d..e9e3369de5 100644--- a/packages/studio/src/captions/generator.ts+++ b/packages/studio/src/captions/generator.ts@@ -8,7 +8,7 @@ import type {CaptionContainerStyle,CaptionShadow,CaptionGlow,-} from "./types.js";+} from "./types";// ---------------------------------------------------------------------------// Public APIdiff --git a/packages/studio/src/captions/parser.test.ts b/packages/studio/src/captions/parser.test.tsindex 36fb68e880..0ce5314d4f 100644--- a/packages/studio/src/captions/parser.test.ts+++ b/packages/studio/src/captions/parser.test.ts@@ -1,7 +1,7 @@// @vitest-environment nodeimport { describe, it, expect } from "vitest";-import { extractTranscript, buildCaptionModel, TranscriptWord } from "./parser.js";-import { DEFAULT_STYLE, DEFAULT_CONTAINER, DEFAULT_ANIMATION_SET } from "./types.js";+import { extractTranscript, buildCaptionModel, TranscriptWord } from "./parser";+import { DEFAULT_STYLE, DEFAULT_CONTAINER, DEFAULT_ANIMATION_SET } from "./types";// ---------------------------------------------------------------------------// Fixturesdiff --git a/packages/studio/src/captions/parser.ts b/packages/studio/src/captions/parser.tsindex 22acddea2d..77e2c43d9f 100644--- a/packages/studio/src/captions/parser.ts+++ b/packages/studio/src/captions/parser.ts@@ -11,7 +11,7 @@ import {DEFAULT_STYLE,DEFAULT_CONTAINER,DEFAULT_ANIMATION_SET,-} from "./types.js";+} from "./types";export interface TranscriptWord {id?: string;diff --git a/packages/studio/src/player/components/Player.tsx b/packages/studio/src/player/components/Player.tsxindex 31c3a8faea..a36e89ad82 100644--- a/packages/studio/src/player/components/Player.tsx+++ b/packages/studio/src/player/components/Player.tsx@@ -1,7 +1,9 @@import { forwardRef, useRef } from "react";import { useMountEffect } from "../../hooks/useMountEffect";import type { HyperframesPlayer } from "@hyperframes/player";-import "@hyperframes/player"; // registers <hyperframes-player> custom element+// NOTE: importing "@hyperframes/player" registers a class extending HTMLElement+// at module load, which throws under SSR. Defer the import to the mount effect+// so it only runs in the browser.interface PlayerProps {projectId?: string;@@ -27,55 +29,68 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(const container = containerRef.current;if (!container) return;- // Create the web component imperatively to avoid JSX custom-element typing.- const player = document.createElement("hyperframes-player") as HyperframesPlayer;- const src = directUrl || `/api/projects/${projectId}/preview`;- player.setAttribute("src", src);- player.setAttribute("width", String(portrait ? 1080 : 1920));- player.setAttribute("height", String(portrait ? 1920 : 1080));- player.style.width = "100%";- player.style.height = "100%";- player.style.display = "block";- container.appendChild(player);+ let canceled = false;+ let cleanup: (() => void) | undefined;- // Bridge the inner iframe to the forwarded ref for useTimelinePlayer.- const iframe = player.iframeElement;- if (typeof ref === "function") {- ref(iframe);- } else if (ref) {- (ref as React.MutableRefObject<HTMLIFrameElement | null>).current = iframe;- }+ // Dynamic import registers the custom element in the browser only.+ import("@hyperframes/player").then(() => {+ if (canceled) return;- // Prevent the web component's built-in click-to-toggle behavior.- // The studio manages playback exclusively via useTimelinePlayer.- const preventToggle = (e: Event) => e.stopImmediatePropagation();- player.addEventListener("click", preventToggle, { capture: true });+ // Create the web component imperatively to avoid JSX custom-element typing.+ const player = document.createElement("hyperframes-player") as HyperframesPlayer;+ const src = directUrl || `/api/projects/${projectId}/preview`;+ player.setAttribute("src", src);+ player.setAttribute("width", String(portrait ? 1080 : 1920));+ player.setAttribute("height", String(portrait ? 1920 : 1080));+ player.style.width = "100%";+ player.style.height = "100%";+ player.style.display = "block";+ container.appendChild(player);- // Forward the iframe's native load event to the studio's onIframeLoad.- const handleLoad = () => {- loadCountRef.current++;- // Reveal animation on reload (hot-reload, composition switch)- if (loadCountRef.current > 1) {- container.classList.remove("preview-revealing");- void container.offsetWidth;- container.classList.add("preview-revealing");- const onEnd = () => container.classList.remove("preview-revealing");- container.addEventListener("animationend", onEnd, { once: true });- }- onLoad();- };- iframe.addEventListener("load", handleLoad);-- return () => {- iframe.removeEventListener("load", handleLoad);- player.removeEventListener("click", preventToggle, { capture: true });- container.removeChild(player);- // Clear the forwarded ref+ // Bridge the inner iframe to the forwarded ref for useTimelinePlayer.+ const iframe = player.iframeElement;if (typeof ref === "function") {- ref(null);+ ref(iframe);} else if (ref) {- (ref as React.MutableRefObject<HTMLIFrameElement | null>).current = null;+ (ref as React.MutableRefObject<HTMLIFrameElement | null>).current = iframe;}++ // Prevent the web component's built-in click-to-toggle behavior.+ // The studio manages playback exclusively via useTimelinePlayer.+ const preventToggle = (e: Event) => e.stopImmediatePropagation();+ player.addEventListener("click", preventToggle, { capture: true });++ // Forward the iframe's native load event to the studio's onIframeLoad.+ const handleLoad = () => {+ loadCountRef.current++;+ // Reveal animation on reload (hot-reload, composition switch)+ if (loadCountRef.current > 1) {+ container.classList.remove("preview-revealing");+ void container.offsetWidth;+ container.classList.add("preview-revealing");+ const onEnd = () => container.classList.remove("preview-revealing");+ container.addEventListener("animationend", onEnd, { once: true });+ }+ onLoad();+ };+ iframe.addEventListener("load", handleLoad);++ cleanup = () => {+ iframe.removeEventListener("load", handleLoad);+ player.removeEventListener("click", preventToggle, { capture: true });+ container.removeChild(player);+ // Clear the forwarded ref+ if (typeof ref === "function") {+ ref(null);+ } else if (ref) {+ (ref as React.MutableRefObject<HTMLIFrameElement | null>).current = null;+ }+ };+ });++ return () => {+ canceled = true;+ cleanup?.();};});