Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add social crawler handling and OG preview support

Richie McIlroy1a7480ad9f95
diff --git a/apps/web/app/robots.ts b/apps/web/app/robots.ts
index db2b219e4..c4d4f6357 100644
--- a/apps/web/app/robots.ts
+++ b/apps/web/app/robots.ts
@@ -1,26 +1,18 @@
import type { MetadataRoute } from "next";
import { headers } from "next/headers";
import { seoPages } from "@/lib/seo-pages";
+import {
+ SOCIAL_CRAWLER_ROBOTS_USER_AGENTS,
+ SOCIAL_REFERRER_DOMAINS,
+} from "@/lib/social-crawlers";
export default async function robots(): Promise<MetadataRoute.Robots> {
const seoPageSlugs = Object.keys(seoPages);
const headersList = await headers();
- const referrer = headersList.get("x-referrer") || "";
+ const referrer =
+ headersList.get("x-referrer") || headersList.get("referer") || "";
- const allowedReferrers = [
- "x.com",
- "facebook.com",
- "fb.com",
- "linkedin.com",
- "slack.com",
- "notion.so",
- "reddit.com",
- "youtube.com",
- "quora.com",
- "t.co",
- ];
-
- const isAllowedReferrer = allowedReferrers.some((domain) =>
+ const isAllowedReferrer = SOCIAL_REFERRER_DOMAINS.some((domain) =>
referrer.includes(domain),
);
@@ -32,13 +24,19 @@ export default async function robots(): Promise<MetadataRoute.Robots> {
"/record",
"/home",
];
+ const searchDisallowPaths = [...disallowPaths];
if (!isAllowedReferrer) {
- disallowPaths.push("/s/*");
+ searchDisallowPaths.push("/s/*");
}
return {
rules: [
+ ...SOCIAL_CRAWLER_ROBOTS_USER_AGENTS.map((userAgent) => ({
+ userAgent,
+ allow: ["/", "/s/*"],
+ disallow: disallowPaths,
+ })),
{
userAgent: "*",
allow: [
@@ -47,7 +45,7 @@ export default async function robots(): Promise<MetadataRoute.Robots> {
...seoPageSlugs.map((slug) => `/${slug}`),
...(isAllowedReferrer ? ["/s/*"] : []),
],
- disallow: disallowPaths,
+ disallow: searchDisallowPaths,
},
],
sitemap: "https://cap.so/sitemap.xml",
diff --git a/apps/web/app/s/[videoId]/page.tsx b/apps/web/app/s/[videoId]/page.tsx
index ef451fa83..bd1d890e6 100644
--- a/apps/web/app/s/[videoId]/page.tsx
+++ b/apps/web/app/s/[videoId]/page.tsx
@@ -42,6 +42,10 @@ import {
import { createNotification } from "@/lib/Notification";
import * as EffectRuntime from "@/lib/server";
import { runPromise } from "@/lib/server";
+import {
+ isSocialCrawlerUserAgent,
+ SOCIAL_REFERRER_DOMAINS,
+} from "@/lib/social-crawlers";
import { transcribeVideo } from "@/lib/transcribe";
import { optionFromTOrFirst } from "@/utils/effect";
import { isAiGenerationEnabled } from "@/utils/flags";
@@ -106,16 +110,6 @@ async function getSharedSpacesForVideo(videoId: Video.VideoId) {
return sharedSpaces;
}
-const ALLOWED_REFERRERS = [
- "x.com",
- "twitter.com",
- "facebook.com",
- "fb.com",
- "slack.com",
- "notion.so",
- "linkedin.com",
-];
-
function PolicyDeniedView({ reason }: { reason?: string }) {
let title = "This video is private";
let description: React.ReactNode = (
@@ -166,10 +160,15 @@ export async function generateMetadata(
const params = await props.params;
const videoId = params.videoId as Video.VideoId;
- const referrer = (await headers()).get("x-referrer") || "";
- const isAllowedReferrer = ALLOWED_REFERRERS.some((domain) =>
+ const headersList = await headers();
+ const referrer =
+ headersList.get("x-referrer") || headersList.get("referer") || "";
+ const requestUserAgent = headersList.get("user-agent") || "";
+ const isAllowedReferrer = SOCIAL_REFERRER_DOMAINS.some((domain) =>
referrer.includes(domain),
);
+ const canRenderSocialPreview =
+ isAllowedReferrer || isSocialCrawlerUserAgent(requestUserAgent);
return Effect.flatMap(Videos, (v) => v.getByIdForViewing(videoId)).pipe(
Effect.map(
@@ -180,6 +179,10 @@ export async function generateMetadata(
`/api/video/preview?videoId=${videoId}&fallback=og`,
buildEnv.NEXT_PUBLIC_WEB_URL,
).toString();
+ const ogImageUrl = new URL(
+ `/api/video/og?videoId=${videoId}`,
+ buildEnv.NEXT_PUBLIC_WEB_URL,
+ ).toString();
const playlistUrl = new URL(
`/api/playlist?videoId=${video.id}`,
buildEnv.NEXT_PUBLIC_WEB_URL,
@@ -189,7 +192,19 @@ export async function generateMetadata(
title: `${video.name} | Cap Recording`,
description: "Watch this video on Cap",
openGraph: {
- images: [{ url: previewImageUrl }],
+ images: [
+ {
+ url: previewImageUrl,
+ width: 480,
+ height: 270,
+ type: "image/gif",
+ },
+ {
+ url: ogImageUrl,
+ width: 1200,
+ height: 630,
+ },
+ ],
videos: [
{
url: playlistUrl,
@@ -226,7 +241,9 @@ export async function generateMetadata(
height: 720,
},
},
- robots: isAllowedReferrer ? "index, follow" : "noindex, nofollow",
+ robots: canRenderSocialPreview
+ ? "index, follow"
+ : "noindex, nofollow",
};
},
}),
diff --git a/apps/web/lib/social-crawlers.ts b/apps/web/lib/social-crawlers.ts
new file mode 100644
index 000000000..93fc7fc31
--- /dev/null
+++ b/apps/web/lib/social-crawlers.ts
@@ -0,0 +1,52 @@
+const SOCIAL_CRAWLER_USER_AGENT_PATTERNS = [
+ "twitterbot",
+ "facebookexternalhit",
+ "facebot",
+ "slackbot",
+ "linkedinbot",
+ "whatsapp",
+ "discordbot",
+ "telegrambot",
+ "skypeuripreview",
+ "microsoftpreview",
+ "pinterestbot",
+ "redditbot",
+ "embedly",
+] as const;
+
+export const SOCIAL_REFERRER_DOMAINS = [
+ "x.com",
+ "twitter.com",
+ "facebook.com",
+ "fb.com",
+ "slack.com",
+ "notion.so",
+ "linkedin.com",
+ "reddit.com",
+ "youtube.com",
+ "quora.com",
+ "t.co",
+] as const;
+
+export const SOCIAL_CRAWLER_ROBOTS_USER_AGENTS = [
+ "Twitterbot",
+ "facebookexternalhit",
+ "Facebot",
+ "Slackbot-LinkExpanding",
+ "LinkedInBot",
+ "WhatsApp",
+ "Discordbot",
+ "TelegramBot",
+ "SkypeUriPreview",
+ "MicrosoftPreview",
+ "Pinterestbot",
+ "redditbot",
+ "Embedly",
+] as const;
+
+export function isSocialCrawlerUserAgent(userAgent: string) {
+ const normalizedUserAgent = userAgent.toLowerCase();
+ return SOCIAL_CRAWLER_USER_AGENT_PATTERNS.some((pattern) =>
+ normalizedUserAgent.includes(pattern),
+ );
+}