try {
const html = await cloudflareClient.browserRendering.content.create({
account_id: process.env.CLOUDFLARE_ACCOUNT_ID!,
url,
rejectResourceTypes: ["image", "media", "font", "stylesheet"],
actionTimeout: 6000,
waitForSelector: {
selector: "meta[property='og:title'], meta[name='twitter:title']",
timeout: 3000,
},
});
const $ = cheerio.load(html);
const image =
$('meta[name="twitter:image"]').attr("content") ||
$('meta[property="og:image"]').attr("content") ||
$('meta[property="og:image:url"]').attr("content");
const imageAlt =
$('meta[name="twitter:image:alt"]').attr("content") ||
$('meta[property="og:image:alt"]').attr("content");
const title =
$('meta[name="twitter:title"]').attr("content") ||
$('meta[property="og:title"]').attr("content");
const description =
$('meta[name="twitter:description"]').attr("content") ||
$('meta[property="og:description"]').attr("content");
const site =
$('meta[name="twitter:site"]').attr("content") ||
$('meta[property="og:site_name"]').attr("content") ||
$('meta[name="application-name"]').attr("content");
const type =
$('meta[name="twitter:card"]').attr("content") ||
$('meta[property="og:type"]').attr("content");
previewLinkMeta = { image, title, description, site, url, imageAlt, type };
if (previewLinkMeta?.title || previewLinkMeta?.image) {
return await setupClient(token).mutation(
api.utils.setPreviewLinkMeta,
previewLinkMeta,
);
}
} catch (error) {
console.error("Both OpenGraph and fallback failed:", error);
throw new Error("Failed to fetch fallback data");
}