aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docs/pages/api/og.tsx
blob: 196ca7202621e6970871756baf8aeddce4c15bbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React, { createElement } from "react";
import { ImageResponse } from "@vercel/og";

import PackLogo from "../../components/logos/og/PackLogo";
import RepoLogo from "../../components/logos/og/RepoLogo";
import TurboLogo from "../../components/logos/og/TurboLogo";
import VercelLogo from "../../components/logos/og/VercelLogo";

import type { NextApiRequest } from "next/index";

function _arrayBufferToBase64(buffer) {
  var binary = "";
  var bytes = new Uint8Array(buffer);
  var len = bytes.byteLength;
  for (var i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i]);
  }
  return btoa(binary);
}

async function loadAssets(): Promise<
  [
    { name: string; data: ArrayBuffer; weight: 400 | 700; style: "normal" }[],
    string
  ]
> {
  const [inter, spaceMono, bg] = await Promise.all([
    fetch(
      String(new URL("../../assets/inter-v12-latin-700.ttf", import.meta.url))
    ).then((res) => res.arrayBuffer()),
    fetch(
      String(
        new URL(
          "../../assets/space-mono-v12-latin-regular.ttf",
          import.meta.url
        )
      )
    ).then((res) => res.arrayBuffer()),
    fetch(String(new URL("../../assets/bg.jpeg", import.meta.url))).then(
      (res) => res.arrayBuffer()
    ),
  ]);
  return [
    [
      {
        name: "Inter",
        data: inter,
        weight: 700 as const,
        style: "normal" as const,
      },
      {
        name: "Space Mono",
        data: spaceMono,
        weight: 400 as const,
        style: "normal" as const,
      },
    ],
    _arrayBufferToBase64(bg),
  ];
}

export const config = {
  runtime: "experimental-edge",
};

export default async function openGraphImage(
  req: NextApiRequest
): Promise<ImageResponse> {
  try {
    const [fonts, bg] = await loadAssets();
    const { searchParams } = new URL(req.url);

    const type = searchParams.get("type");

    // ?title=<title>
    const hasTitle = searchParams.has("title");
    const title = hasTitle
      ? searchParams.get("title")?.slice(0, 100)
      : type === "pack"
      ? "The successor to Webpack"
      : type === "repo"
      ? "The build system that makes ship happen"
      : "";

    return new ImageResponse(createElement(OGImage, { title, type, bg }), {
      width: 1200,
      height: 630,
      fonts,
    });
  } catch (e: unknown) {
    return new Response(undefined, {
      status: 302,
      headers: {
        Location: "https://turbo.build/og-image.png",
      },
    });
  }
}

export function OGImage({
  title,
  type,
  bg,
}: {
  title: string;
  type: string;
  bg: string;
}): JSX.Element {
  return (
    <div
      style={{
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        justifyContent: "center",
        width: "100%",
        height: "100%",
        fontFamily: "Inter",
        fontWeight: 700,
        fontSize: 60,
        backgroundImage: `url(data:image/jpeg;base64,${bg})`,
        backgroundSize: "1200px 630px",
        color: "#fff",
      }}
    >
      {/* eslint-disable-next-line  @next/next/no-img-element, jsx-a11y/alt-text */}
      <div style={{ display: "flex", height: 97 * 1.1, alignItems: "center" }}>
        {type === "pack" ? (
          <PackLogo height={103 * 1.1} width={697 * 1.1} />
        ) : type === "repo" ? (
          <RepoLogo height={83 * 1.1} width={616 * 1.1} />
        ) : (
          <TurboLogo height={97 * 1.1} width={459 * 1.1} />
        )}
      </div>
      {title ? (
        <div
          style={{
            fontFamily: "Space Mono",
            fontSize: 36,
            letterSpacing: -1.5,
            padding: "15px 20px 30px",
            textAlign: "center",
            backgroundImage: "linear-gradient(to bottom, #fff, #aaa)",
            backgroundClip: "text",
            color: "transparent",
          }}
        >
          {title}
        </div>
      ) : null}
      <div
        style={{
          fontFamily: "Space Mono",
          fontSize: 18,
          marginTop: 80,
          display: "flex",
          color: "#fff",
          alignItems: "center",
        }}
      >
        <div style={{ marginRight: 12 }}>by</div>
        <VercelLogo fill="white" height={30} />
      </div>
    </div>
  );
}