Open source CapCut alternative.
TypeScript
fix(site): load every Cut media URL with CORS
David Led841f436aca8
diff --git a/site/src/cut/components/AssetRefs.tsx b/site/src/cut/components/AssetRefs.tsxindex e2cce1c6..0d77b47e 100644--- a/site/src/cut/components/AssetRefs.tsx+++ b/site/src/cut/components/AssetRefs.tsx@@ -8,6 +8,7 @@ import {refToken,type AssetRef,} from "@/cut/lib/assetRef";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { useRefFor, useRefCandidates } from "@/cut/lib/assetRef";import { useInView } from "@/cut/hooks/useInView";import { revealRef } from "@/cut/lib/refReveal";@@ -44,7 +45,7 @@ export function RefThumb({ item, className }: { item: AssetRef; className?: stri)}>{item.kind === "video" ? (- <video+ <video crossOrigin={MEDIA_CORS}src={seen ? `${item.url}#t=0.1` : undefined}preload="metadata"muted@@ -54,7 +55,7 @@ export function RefThumb({ item, className }: { item: AssetRef; className?: stri) : item.kind === "image" ? (seen ? (// eslint-disable-next-line @next/next/no-img-element -- refs point at engine/static files, not Next-optimizable images- <img src={item.url} alt={item.name} loading="lazy" className="size-full object-cover" />+ <img crossOrigin={MEDIA_CORS} src={item.url} alt={item.name} loading="lazy" className="size-full object-cover" />) : null) : item.kind === "text" ? (<div className="grid size-full place-items-center bg-gradient-to-br from-slate-100 to-slate-50 text-slate-500">@@ -138,7 +139,7 @@ function PeekVideo({ item }: { item: AssetRef }) {}, []);return (<div className="relative aspect-square w-full border border-border bg-muted">- <video ref={videoRef} src={item.url} loop playsInline className="size-full object-cover" />+ <video crossOrigin={MEDIA_CORS} ref={videoRef} src={item.url} loop playsInline className="size-full object-cover" />{item.duration !== undefined && (<span className="absolute right-1 bottom-1 rounded-[5px] bg-black/65 px-1 py-px font-mono text-[8.5px] text-white tabular-nums">{formatTime(item.duration)}diff --git a/site/src/cut/components/ChatAssets.tsx b/site/src/cut/components/ChatAssets.tsxindex e313caed..bc11aad5 100644--- a/site/src/cut/components/ChatAssets.tsx+++ b/site/src/cut/components/ChatAssets.tsx@@ -4,6 +4,7 @@ import { useEffect, useState, type ReactNode } from "react";import type React from "react";import { Copy, ExternalLink, FileText, Film, Loader2, Maximize2, Plus } from "lucide-react";import { DropdownMenuItem } from "@/components/ui/dropdown-menu";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { clearAssetDrag, setAssetDragData, setChipDragImage } from "@/cut/lib/assetDrag";import {projectRefs,@@ -260,7 +261,7 @@ function MediaCard({ item, asset }: ChatCardProps) {>{item.kind === "video" ? (// Native first frame as the poster — full-resolution, no blurry thumb.- <video+ <video crossOrigin={MEDIA_CORS}ref={videoRef}src={seen ? `${ref.url}#t=0.1` : undefined}preload="metadata"@@ -271,7 +272,7 @@ function MediaCard({ item, asset }: ChatCardProps) {/>) : seen ? (// eslint-disable-next-line @next/next/no-img-element -- engine/static file, not Next-optimizable- <img src={ref.url} alt={ref.name} loading="lazy" className="size-full object-cover" />+ <img crossOrigin={MEDIA_CORS} src={ref.url} alt={ref.name} loading="lazy" className="size-full object-cover" />) : null}{item.kind === "video" && ref.duration !== undefined && (<span className="absolute right-1.5 bottom-1.5 rounded-[5px] bg-black/65 px-1 py-px font-mono text-[9px] text-white tabular-nums">diff --git a/site/src/cut/components/GeneratePanel.tsx b/site/src/cut/components/GeneratePanel.tsxindex 74253f19..369093ac 100644--- a/site/src/cut/components/GeneratePanel.tsx+++ b/site/src/cut/components/GeneratePanel.tsx@@ -4,6 +4,7 @@ import { useEffect, useRef } from "react";import { Copy, Film, Loader2, Maximize2, Plus, Sparkles, Trash2, X } from "lucide-react";import { Button } from "@/components/ui/button";import { DropdownMenuItem } from "@/components/ui/dropdown-menu";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { clearAssetDrag, setAssetDragData } from "@/cut/lib/assetDrag";import {collectRefs,@@ -344,6 +345,7 @@ function JobRow({ job, handle }: { job: GenerateJob; handle?: string }) {onClick={() => useVideoGen.getState().openWith(job.prompt)}><video+ crossOrigin={MEDIA_CORS}ref={videoRef}mutedloopdiff --git a/site/src/cut/components/ImageGenPanel.tsx b/site/src/cut/components/ImageGenPanel.tsxindex 8cfedf53..234550e5 100644--- a/site/src/cut/components/ImageGenPanel.tsx+++ b/site/src/cut/components/ImageGenPanel.tsx@@ -5,6 +5,7 @@ import { Copy, Loader2, Maximize2, Sparkles, Trash2 } from "lucide-react";import { Button } from "@/components/ui/button";import { DropdownMenuItem } from "@/components/ui/dropdown-menu";import { SectionTitle } from "@/cut/components/SectionTitle";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { clearAssetDrag, setAssetDragData } from "@/cut/lib/assetDrag";import { collectRefs, mentionToken, useRefCandidates, useAssetDrop } from "@/cut/lib/assetRef";import { genPulseOverlay, useGenPulse } from "@/cut/lib/genNotify";@@ -251,7 +252,7 @@ function GeneratedTile({onClick={() => useImageGen.getState().openWith(asset.name)}>{/* eslint-disable-next-line @next/next/no-img-element -- engine media file, not Next-optimizable */}- <img+ <img crossOrigin={MEDIA_CORS}src={asset.url}alt={asset.name}loading="lazy"diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex 3f875885..d9bfc318 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -31,6 +31,7 @@ import {DialogTitle,} from "@/components/ui/dialog";import { Input } from "@/components/ui/input";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { clearAssetDrag, setCardDragImage, setLibraryDragData } from "@/cut/lib/assetDrag";import { useInView } from "@/cut/hooks/useInView";import { isMediaFile } from "@/cut/lib/media";@@ -514,6 +515,7 @@ export function LibraryCard({{a.type === "video" ? (seen && (<video+ crossOrigin={MEDIA_CORS}ref={videoRef}src={`${libraryMediaUrl(a.fileName)}#t=${posterT}`}muted@@ -525,7 +527,7 @@ export function LibraryCard({)) : a.type === "image" ? (// eslint-disable-next-line @next/next/no-img-element -- library media file, not Next-optimizable- <img src={libraryMediaUrl(a.fileName)} alt={a.name} loading="lazy" className="size-full object-cover" />+ <img crossOrigin={MEDIA_CORS} src={libraryMediaUrl(a.fileName)} alt={a.name} loading="lazy" className="size-full object-cover" />) : (<AudioCardFaceurl={libraryMediaUrl(a.fileName)}diff --git a/site/src/cut/components/Lightbox.tsx b/site/src/cut/components/Lightbox.tsxindex 858206ab..f5f47025 100644--- a/site/src/cut/components/Lightbox.tsx+++ b/site/src/cut/components/Lightbox.tsx@@ -3,6 +3,7 @@import { useEffect, useRef, useState } from "react";import { Check, Copy, FileText, Loader2, Music, Plus, X } from "lucide-react";import { Button } from "@/components/ui/button";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { addLibraryAssetToProject, fetchLibrary } from "@/cut/lib/library";import { useLightbox, type LightboxItem } from "@/cut/lib/lightbox";import { importImage, importStockVideo } from "@/cut/lib/media";@@ -176,7 +177,7 @@ function LightboxMedia({ item, ratio }: { item: LightboxItem; ratio?: number })if (item.kind === "video") {return (- <video+ <video crossOrigin={MEDIA_CORS}controlsautoPlayloop@@ -189,7 +190,7 @@ function LightboxMedia({ item, ratio }: { item: LightboxItem; ratio?: number })}return (// eslint-disable-next-line @next/next/no-img-element -- static/project image, client-only page- <img src={item.src} alt={item.name} className={mediaClass} style={mediaStyle} />+ <img crossOrigin={MEDIA_CORS} src={item.src} alt={item.name} className={mediaClass} style={mediaStyle} />);}diff --git a/site/src/cut/components/PlatformPreview.tsx b/site/src/cut/components/PlatformPreview.tsxindex e79fa2a4..c7aebf17 100644--- a/site/src/cut/components/PlatformPreview.tsx+++ b/site/src/cut/components/PlatformPreview.tsx@@ -1,6 +1,7 @@"use client";import { useEffect, useState } from "react";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { apiUrl } from "@/cut/lib/backend";import {Bookmark,@@ -117,6 +118,7 @@ export function PlatformPreviewDialog({)}><video+ crossOrigin={MEDIA_CORS}src={url}// Original leaves width and height auto so the element takes the// video's intrinsic aspect, bounded by the viewport.diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsxindex 939b87d0..4e60b479 100644--- a/site/src/cut/components/ProjectsHome.tsx+++ b/site/src/cut/components/ProjectsHome.tsx@@ -46,6 +46,7 @@ import {DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";import { Input } from "@/components/ui/input";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { engineOrigin, servedFromEngine } from "@/cut/lib/api";import { quotaErrorMessage } from "@/cut/lib/backend/cloud";import { useCloudUsage, useCutMode } from "@/cut/lib/backend/hooks";@@ -962,6 +963,7 @@ function CardPreview({ project: p, residency }: { project: ProjectSummary; residreturn (// eslint-disable-next-line @next/next/no-img-element -- engine media file, not Next-optimizable<img+ crossOrigin={MEDIA_CORS}src={fileUrl(p.previewFile!)}alt=""loading="lazy"@@ -989,6 +991,7 @@ function CardPreview({ project: p, residency }: { project: ProjectSummary; resid>{seen && (<video+ crossOrigin={MEDIA_CORS}ref={videoRef}src={`${src}#t=${posterT}`}muteddiff --git a/site/src/cut/components/SceneCard.tsx b/site/src/cut/components/SceneCard.tsxindex 03d90a79..b52691f9 100644--- a/site/src/cut/components/SceneCard.tsx+++ b/site/src/cut/components/SceneCard.tsx@@ -26,6 +26,7 @@ import { cn } from "@/lib/utils";import { HostedErrorText } from "./hostedError";import { scrimIconButton } from "./iconButton";import { RefThumb } from "./AssetRefs";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";// The brief-to-video progress card, pinned into the chat while a "generate a// video" run is planning, waiting for approval, or rendering. The timeline fills@@ -326,7 +327,7 @@ function ShotTile({{...(clip && ref ? hoverPlay : {})}>{clip && ref ? (- <video+ <video crossOrigin={MEDIA_CORS}ref={videoRef}src={`${ref.url}#t=0.1`}preload="metadata"@@ -337,7 +338,7 @@ function ShotTile({/>) : frame && ref ? (// eslint-disable-next-line @next/next/no-img-element -- engine/static file, not Next-optimizable- <img src={ref.url} alt="" className="size-full object-cover" />+ <img crossOrigin={MEDIA_CORS} src={ref.url} alt="" className="size-full object-cover" />) : (<span className="grid size-full place-items-center text-[13px] font-semibold text-muted-foreground/50">{inFlight ? <CircleDashed className="size-4 animate-spin text-[#0a84ff]" /> : n}@@ -483,7 +484,7 @@ function FeedEntry({{...(item.kind === "video" ? hoverPlay : {})}>{item.kind === "video" ? (- <video+ <video crossOrigin={MEDIA_CORS}ref={videoRef}src={`${item.url}#t=0.1`}preload="metadata"@@ -494,7 +495,7 @@ function FeedEntry({/>) : (// eslint-disable-next-line @next/next/no-img-element -- engine/static file, not Next-optimizable- <img src={item.url} alt={item.name} className="size-full object-cover" />+ <img crossOrigin={MEDIA_CORS} src={item.url} alt={item.name} className="size-full object-cover" />)}{item.kind === "video" && item.duration !== undefined && (<span className="absolute right-1.5 bottom-1.5 rounded-[5px] bg-black/65 px-1 py-px font-mono text-[9px] text-white tabular-nums">diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsxindex 14abc06d..a8712c1e 100644--- a/site/src/cut/components/SidePanel.tsx+++ b/site/src/cut/components/SidePanel.tsx@@ -23,6 +23,7 @@ import {DropdownMenuSeparator,DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { apiFetch, apiUrl } from "@/cut/lib/backend";import { useCutCaps } from "@/cut/lib/backend/hooks";import {@@ -590,7 +591,7 @@ function MediaPanel({title="Preview as a post"onClick={() => setPreview(it)}>- <video+ <video crossOrigin={MEDIA_CORS}mutedplaysInlinepreload="metadata"@@ -758,7 +759,7 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string{asset.type === "video" ? (// Native first frame as the poster — full-resolution, no blurry thumb.seen && (- <video+ <video crossOrigin={MEDIA_CORS}ref={videoRef}src={`${asset.url}#t=0.1`}preload="metadata"@@ -770,7 +771,7 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string)) : asset.type === "image" ? (// eslint-disable-next-line @next/next/no-img-element -- engine/static file, not Next-optimizable- <img src={asset.url} alt={asset.name} loading="lazy" className="size-full object-cover" />+ <img crossOrigin={MEDIA_CORS} src={asset.url} alt={asset.name} loading="lazy" className="size-full object-cover" />) : (<AudioCardFaceurl={asset.url}diff --git a/site/src/cut/components/TemplateCard.tsx b/site/src/cut/components/TemplateCard.tsxindex 217a07b7..d80cedb7 100644--- a/site/src/cut/components/TemplateCard.tsx+++ b/site/src/cut/components/TemplateCard.tsx@@ -27,6 +27,7 @@ import {setCardDragImage,setTemplateDragData,} from "@/cut/lib/assetDrag";+import { MEDIA_CORS } from "@/cut/lib/mediaCors";import { useAssetDrop, type AssetRef } from "@/cut/lib/assetRef";import { cardIconButton } from "@/cut/components/iconButton";import { formatTime } from "@/cut/lib/time";@@ -280,6 +281,7 @@ export function TemplateCard({>{preview.media.type === "video" ? (<video+ crossOrigin={MEDIA_CORS}src={mediaSrc(preview.media.fileName)}autoPlaymuted@@ -290,6 +292,7 @@ export function TemplateCard({) : (// eslint-disable-next-line @next/next/no-img-element -- engine media file, not Next-optimizable<img+ crossOrigin={MEDIA_CORS}src={mediaSrc(preview.media.fileName)}alt={preview.media.name}className="w-44 rounded-lg border border-border shadow-lg"diff --git a/site/src/cut/lib/mediaCors.ts b/site/src/cut/lib/mediaCors.tsnew file mode 100644index 00000000..673f6efc--- /dev/null+++ b/site/src/cut/lib/mediaCors.ts@@ -0,0 +1,22 @@+// Cut media is loaded with CORS everywhere, by every consumer, without+// exception — this constant is that rule, and it exists to be impossible to+// forget.+//+// The preview engine, the filmstrips and the AI frame grabs all read pixels+// back off a canvas, so their decoders load `crossOrigin="anonymous"`. A plain+// <video>/<img> pointed at the same file sends no Origin, and R2 answers an+// origin-less request with no CORS headers and — the part that bites — no+// `Vary: Origin`. The browser caches that response as the one answer for the+// URL, so a decoder that asks for it afterwards is handed a body it must+// reject on the CORS check: MEDIA_ERR_SRC_NOT_SUPPORTED, a clip that plays+// black for the life of the tab, and no amount of retrying or re-minting+// clears it. Whichever kind of load reaches a file first wins its cache entry,+// which is why it used to strike some clips in a project and not others.+//+// Loading one way everywhere removes the race: every response carries+// `Vary: Origin` and is cached against the origin that asked for it.+//+// Cut-hosted media only — engine files, project media, library files, exports.+// Stock and other remote media is somebody else's CORS policy, so it keeps+// loading plainly; nothing reads its pixels back.+export const MEDIA_CORS = "anonymous";