a tool for shared writing and social publishing
1import { headers } from "next/headers"; 2 3export async function getMicroLinkOgImage( 4 path: string, 5 options?: { 6 width?: number; 7 height?: number; 8 deviceScaleFactor?: number; 9 noCache?: boolean; 10 }, 11) { 12 const headersList = await headers(); 13 let hostname = headersList.get("x-forwarded-host"); 14 let protocol = headersList.get("x-forwarded-proto"); 15 if (process.env.NODE_ENV === "development") { 16 protocol === "https"; 17 hostname = "leaflet.pub"; 18 } 19 let full_path = `${protocol}://${hostname}${path}`; 20 return getWebpageImage(full_path, options); 21} 22 23export async function getWebpageImage( 24 url: string, 25 options?: { 26 width?: number; 27 height?: number; 28 deviceScaleFactor?: number; 29 noCache?: boolean; 30 }, 31) { 32 let response = await fetch( 33 `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT}/browser-rendering/screenshot`, 34 { 35 method: "POST", 36 headers: { 37 "Content-type": "application/json", 38 Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`, 39 }, 40 body: JSON.stringify({ 41 url, 42 scrollPage: true, 43 addStyleTag: [ 44 { 45 content: `* {scrollbar-width:none; }`, 46 }, 47 ], 48 gotoOptions: { 49 waitUntil: "load", 50 }, 51 viewport: { 52 width: options?.width || 1400, 53 height: options?.height || 733, 54 deviceScaleFactor: options?.deviceScaleFactor, 55 }, 56 }), 57 next: !options?.noCache 58 ? undefined 59 : { 60 revalidate: 600, 61 }, 62 }, 63 ); 64 65 return response; 66}