Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: CORS fixes

Richie McIlroy220deae82c6c
diff --git a/apps/web/app/api/playlist/route.ts b/apps/web/app/api/playlist/route.ts
index 2c8070212..a4077db65 100644
--- a/apps/web/app/api/playlist/route.ts
+++ b/apps/web/app/api/playlist/route.ts
@@ -96,27 +96,45 @@ export async function GET(request: NextRequest) {
{ expiresIn: 3600 }
);
- // Check if this is a direct access from a third-party site by examining the Referer header
- const referer = request.headers.get("referer");
- const isEmbedded = referer && !referer.includes(request.headers.get("host") || "");
-
+ // Set very permissive CORS headers to ensure cross-origin access works
const corsHeaders = {
- ...getHeaders(origin),
- ...CACHE_CONTROL_HEADERS,
"Access-Control-Allow-Origin": "*",
- "Access-Control-Allow-Methods": "GET, OPTIONS",
- "Access-Control-Allow-Headers": "Content-Type, Authorization",
+ "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS",
+ "Access-Control-Allow-Headers": "Content-Type, Authorization, Range, X-Requested-With",
+ "Access-Control-Expose-Headers": "Content-Length, Content-Range, Accept-Ranges",
"Content-Type": "video/mp4",
"X-Content-Type-Options": "nosniff",
+ "Access-Control-Max-Age": "86400",
+ ...CACHE_CONTROL_HEADERS,
};
- return new Response(null, {
- status: 302,
- headers: {
- ...corsHeaders,
- Location: playlistUrl,
- },
- });
+ // Always proxy the content to avoid CORS issues with S3
+ try {
+ // Proxy the S3 content through our API to avoid CORS issues
+ const s3Response = await fetch(playlistUrl);
+
+ if (!s3Response.ok) {
+ console.error(`S3 responded with ${s3Response.status}: ${s3Response.statusText}`);
+ throw new Error(`S3 error: ${s3Response.status}`);
+ }
+
+ const data = await s3Response.arrayBuffer();
+
+ return new Response(data, {
+ status: 200,
+ headers: corsHeaders
+ });
+ } catch (error) {
+ console.error("Error proxying S3 content:", error);
+ // Fall back to redirect if proxying fails
+ return new Response(null, {
+ status: 302,
+ headers: {
+ ...corsHeaders,
+ Location: playlistUrl,
+ },
+ });
+ }
}
const playlistUrl = await getSignedUrl(
diff --git a/apps/web/app/s/[videoId]/_components/MP4VideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/MP4VideoPlayer.tsx
index 0ec42108d..863823faf 100644
--- a/apps/web/app/s/[videoId]/_components/MP4VideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/MP4VideoPlayer.tsx
@@ -59,23 +59,19 @@ export const MP4VideoPlayer = memo(
? `${videoSrc}&_t=${timestamp}`
: `${videoSrc}?_t=${timestamp}`;
- const response = await fetch(urlWithTimestamp, { method: "HEAD" });
-
- if (response.redirected) {
- // If the API redirected us, use the redirected URL
- setCurrentSrc(response.url);
- return response.url;
- } else {
- // If no redirect (which is unusual for desktopMP4), just use the original URL
- return urlWithTimestamp;
- }
+ // With the updated API, we can now use the original URL directly
+ // Our API proxies the content rather than redirecting
+ setCurrentSrc(urlWithTimestamp);
+ return urlWithTimestamp;
} catch (error) {
- console.error("Error fetching new video URL:", error);
- // Return the original URL with timestamp if fetch fails
+ console.error("Error updating video URL:", error);
+ // Return the original URL with timestamp if anything fails
const timestamp = new Date().getTime();
- return videoSrc.includes("?")
+ const fallbackUrl = videoSrc.includes("?")
? `${videoSrc}&_t=${timestamp}`
: `${videoSrc}?_t=${timestamp}`;
+ setCurrentSrc(fallbackUrl);
+ return fallbackUrl;
}
}, [videoSrc]);
diff --git a/apps/web/utils/helpers.ts b/apps/web/utils/helpers.ts
index 114b76bcd..cfd361c19 100644
--- a/apps/web/utils/helpers.ts
+++ b/apps/web/utils/helpers.ts
@@ -8,6 +8,7 @@ export function classNames(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
+// Base allowed origins
export const allowedOrigins = [
clientEnv.NEXT_PUBLIC_WEB_URL,
"https://cap.link",
@@ -15,12 +16,12 @@ export const allowedOrigins = [
];
export function getHeaders(origin: string) {
+ // Allow "*" for custom domains or allowedOrigins for main domains
return {
- "Access-Control-Allow-Origin": allowedOrigins.includes(origin)
- ? origin
- : "null",
+ "Access-Control-Allow-Origin": origin || "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, OPTIONS",
+ "Access-Control-Allow-Headers": "Content-Type, Authorization",
};
}