From dd8ccae88f44211b7320376a011292ab265b5ac0 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 16 Dec 2025 17:46:07 +0400 Subject: [PATCH] Guard against missing data source in et function Validate input in et(n) to reject when no data source is present. Use rawJson directly when provided; otherwise fetch with ts. When rawJson is provided, set projectId to raw-json instead of the default of the default (custom-scene) fallback. Update useUnicornScene to include rawJson in dependencies and in the init key so changes to rawJson trigger reinitialization. --- src/react/original.js | 14 ++++++++++++-- src/shared/hooks.ts | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/react/original.js b/src/react/original.js index 7b5bfda..bdd7ca0 100644 --- a/src/react/original.js +++ b/src/react/original.js @@ -7467,11 +7467,18 @@ while loading this video: } } function et(n) { + // Ensure we have a valid data source to avoid fetching with a null project id + if (!n.rawJson && !n.projectId && !n.filePath) { + return Promise.reject( + new Error("No project ID, JSON file path, or raw JSON data provided") + ); + } + let e = n.projectId ? n.projectId.split("?")[0] : null, t = n.projectId ? n.projectId.split("?")[1] : null; return new Promise((s, i) => { // NEW: If rawJson is provided, use it directly - const dataPromise = n.rawJson + const dataPromise = n.rawJson ? Promise.resolve(n.rawJson) : ts(e, t, n.filePath, i, n.production); @@ -7501,7 +7508,10 @@ while loading this video: fps: n.fps || a.fps || 60, dpi: d, name: a.name, - projectId: e || (n.filePath && n.filePath.split(".")[0]) || "custom-scene", + projectId: + e || + (n.filePath && n.filePath.split(".")[0]) || + (n.rawJson ? "raw-json" : "custom-scene"), renderingScale: l, element: h, lazyLoad: n.lazyLoad, diff --git a/src/shared/hooks.ts b/src/shared/hooks.ts index 31561ca..69f1822 100644 --- a/src/shared/hooks.ts +++ b/src/shared/hooks.ts @@ -231,14 +231,14 @@ export function useUnicornScene({ // Reset state when projectId or jsonFilePath changes useEffect(() => { - const newKey = `${projectId || ""}-${jsonFilePath || ""}-${scale}-${dpi}-${fps}-${production ? "prod" : "dev"}`; + const newKey = `${projectId || ""}-${jsonFilePath || ""}-${rawJson ? "rawJson" : ""}-${scale}-${dpi}-${fps}-${production ? "prod" : "dev"}`; if (initializationKeyRef.current !== newKey) { hasAttemptedRef.current = false; setInitError(null); isInitializingRef.current = false; initializationKeyRef.current = ""; // Reset the key to allow fresh initialization } - }, [projectId, jsonFilePath, scale, dpi, fps, production]); + }, [projectId, jsonFilePath, rawJson, scale, dpi, fps, production]); return { error: initError }; }