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 const hostname = headersList.get("x-forwarded-host");
14 let protocol = headersList.get("x-forwarded-proto");
15 let full_path = `${protocol}://${hostname}${path}`;
16 return getWebpageImage(full_path, options);
17}
18
19export async function getWebpageImage(
20 url: string,
21 options?: {
22 width?: number;
23 height?: number;
24 deviceScaleFactor?: number;
25 noCache?: boolean;
26 },
27) {
28 let response = await fetch(
29 `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT}/browser-rendering/screenshot`,
30 {
31 method: "POST",
32 headers: {
33 "Content-type": "application/json",
34 Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
35 },
36 body: JSON.stringify({
37 url,
38 scrollPage: true,
39 addStyleTag: [
40 {
41 content: `* {overflow: hidden !important; }`,
42 },
43 ],
44 gotoOptions: {
45 waitUntil: "load",
46 },
47 viewport: {
48 width: options?.width || 1400,
49 height: options?.height || 733,
50 deviceScaleFactor: options?.deviceScaleFactor,
51 },
52 }),
53 next: !options?.noCache
54 ? undefined
55 : {
56 revalidate: 600,
57 },
58 },
59 );
60
61 return response;
62}