Skip to content

Open source CapCut alternative.

TypeScript

fix(site): read a background run's project at its own backend

David Le8ec6ddbb2ce6
diff --git a/site/src/cut/lib/genvideo/docWriter.ts b/site/src/cut/lib/genvideo/docWriter.ts
index 4b8582a6..2f357a17 100644
--- a/site/src/cut/lib/genvideo/docWriter.ts
+++ b/site/src/cut/lib/genvideo/docWriter.ts
@@ -14,7 +14,8 @@
* the doc is just the other end of the same contract.
*/
-import { apiFetch, apiJson, getBackend, type CutBackend } from "../backend";
+import { apiJson, getBackend, type CutBackend } from "../backend";
+import { projectBackend } from "../residency";
import { RENDERS_CAP, storedAssets, useEditor } from "../store";
import { mediaUrl, SPEED_MIN } from "../types";
import type { AudioClip, MediaAsset, ProjectDoc, RenderRecord, VideoClip } from "../types";
@@ -138,11 +139,15 @@ export async function findRunAsset(
const live = s.assets.find((a) => a.id === assetId);
if (live) return live;
}
- const res = await apiFetch(`/api/cut/projects/${projectId}`);
+ // The project's own backend, not the ambient one: a run whose project the
+ // user navigated away from would otherwise ask the engine for a cloud
+ // project's doc (or the reverse) and lose its identity anchors silently.
+ const backend = await projectBackend(projectId);
+ const res = await backend.fetch(`/api/cut/projects/${projectId}`);
const doc = await apiJson<ProjectDoc>(res);
if (!res.ok) return undefined;
const stored = doc.assets.find((a) => a.id === assetId);
- return stored ? { ...stored, url: mediaUrl(projectId, stored.fileName) } : undefined;
+ return stored ? { ...stored, url: mediaUrl(projectId, stored.fileName, backend) } : undefined;
}
/** placeGenClip against a doc: fill [startSec, endSec) exactly on track 0,
diff --git a/site/src/cut/lib/residency.ts b/site/src/cut/lib/residency.ts
index b5c8a265..fa228854 100644
--- a/site/src/cut/lib/residency.ts
+++ b/site/src/cut/lib/residency.ts
@@ -49,6 +49,28 @@ export function activeResidency(): Residency {
return cutMode() === "cloud" ? "cloud" : "local";
}
+const owners = new Map<string, Promise<CutBackend>>();
+
+/** The backend that holds a project's data, by id. Background work — a scene
+ * run that outlives the editor it started in — must address the project
+ * through this rather than the ambient backend: once the user navigates away
+ * the ambient one is whatever the app fell back to, and a cloud project's doc
+ * does not exist on the engine. A project never changes residency in place
+ * (a copy gets a new id), so the answer is remembered — but only a positive
+ * engine hit is definitive, since "cloud" is also what a momentarily
+ * unreachable engine looks like. */
+export function projectBackend(projectId: string): Promise<CutBackend> {
+ let p = owners.get(projectId);
+ if (!p) {
+ p = resolveProjectMode(projectId).then((mode) => {
+ if (mode !== "local") owners.delete(projectId);
+ return backendFor(mode === "local" ? "local" : "cloud");
+ });
+ owners.set(projectId, p);
+ }
+ return p;
+}
+
export async function resolveProjectMode(projectId: string): Promise<CutMode> {
if (!servedFromEngine() && !engineOrigin()) return "cloud";
try {