Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions frontend/src/helpers/social-card.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { promises as fs } from "fs";
import path from "path";

import { ImageResponse } from "@vercel/og";
import type { NextApiResponse } from "next";
import type { ReactElement } from "react";

export type CardType = "social" | "social-square" | "social-twitter";

export const getSize = (cardType: CardType) => {
Expand All @@ -20,3 +27,47 @@ export const getTitleFontSize = (cardType: CardType) => {
return 8;
}
};

export const sendSocialCard = async (
res: NextApiResponse,
card: ReactElement,
) => {
// Literal paths keep Next's file tracing to these assets instead of all of public/.
const [regularFont, semiBoldFont] = await Promise.all([
fs.readFile(
path.join(
process.cwd(),
"public/social-card-font/GeneralSans-Regular.otf",
),
),
fs.readFile(
path.join(
process.cwd(),
"public/social-card-font/GeneralSans-Semibold.otf",
),
),
]);

const image = new ImageResponse(card, {
width: 1200,
height: 630,
fonts: [
{
name: "GeneralSans",
data: regularFont,
style: "normal",
weight: 500,
},
{
name: "GeneralSans",
data: semiBoldFont,
style: "normal",
weight: 600,
},
],
});
const body = Buffer.from(await image.arrayBuffer());

image.headers.forEach((value, key) => res.setHeader(key, value));
res.send(body);
};
73 changes: 14 additions & 59 deletions frontend/src/pages/api/[lang]/event/[slug]/social-card.tsx
Original file line number Diff line number Diff line change
@@ -1,77 +1,32 @@
import { ImageResponse } from "@vercel/og";

import type { NextRequest } from "next/server";
import type { NextApiRequest, NextApiResponse } from "next";

import { createClient } from "~/apollo/create-client";
import { TitleSubtitleCard } from "~/components/social-card-images/title-subtitle-card";
import { sendSocialCard } from "~/helpers/social-card";
import { queryTalk } from "~/types";

export const config = {
runtime: "edge",
unstable_allowDynamic: ["/node_modules/.pnpm/**"],
};

export const handler = async (req: NextRequest) => {
const regularFont = fetch(
new URL(
"./social-card-font/GeneralSans-Regular.otf",
req.url.substring(0, req.url.lastIndexOf("/api")),
),
).then((res) => res.arrayBuffer());
const semiBoldFont = fetch(
new URL(
"./social-card-font/GeneralSans-Semibold.otf",
req.url.substring(0, req.url.lastIndexOf("/api")),
),
).then((res) => res.arrayBuffer());

export const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const client = createClient();
const { searchParams } = new URL(req.url);

const language = searchParams.get("lang");
const slug = searchParams.get("slug");
const language = req.query.lang as string;
const slug = req.query.slug as string;

const [
regularFontData,
semiBoldFontData,
{
data: { conference },
},
] = await Promise.all([
regularFont,
semiBoldFont,
queryTalk(client, {
slug,
language,
code: process.env.conferenceCode,
}),
]);
const {
data: { conference },
} = await queryTalk(client, {
slug,
language,
code: process.env.conferenceCode,
});

const talk = conference?.talk;

const title = talk.title;
const speakers = talk.speakers.map((speaker) => speaker.fullName).join(", ");

return new ImageResponse(
await sendSocialCard(
res,
<TitleSubtitleCard title={title} subtitle={speakers} tag={talk.type} />,
{
width: 1200,
height: 630,
fonts: [
{
name: "GeneralSans",
data: regularFontData,
style: "normal",
weight: 500,
},
{
name: "GeneralSans",
data: semiBoldFontData,
style: "normal",
weight: 600,
},
],
},
);
};

Expand Down
55 changes: 7 additions & 48 deletions frontend/src/pages/api/[lang]/keynotes/[slug]/social-card.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
import { ImageResponse } from "@vercel/og";

import type { NextRequest } from "next/server";
import type { NextApiRequest, NextApiResponse } from "next";

import { createClient } from "~/apollo/create-client";
import { TitleSubtitleCard } from "~/components/social-card-images/title-subtitle-card";
import { sendSocialCard } from "~/helpers/social-card";
import { queryTalk } from "~/types";

export const config = {
runtime: "edge",
unstable_allowDynamic: ["/node_modules/.pnpm/**"],
};

export const handler = async (req: NextRequest) => {
const regularFont = fetch(
new URL(
"./social-card-font/GeneralSans-Regular.otf",
req.url.substring(0, req.url.lastIndexOf("/api")),
),
).then((res) => res.arrayBuffer());
const semiBoldFont = fetch(
new URL(
"./social-card-font/GeneralSans-Semibold.otf",
req.url.substring(0, req.url.lastIndexOf("/api")),
),
).then((res) => res.arrayBuffer());

const [regularFontData, semiBoldFontData] = await Promise.all([
regularFont,
semiBoldFont,
]);
export const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const client = createClient();
const { searchParams } = new URL(req.url);

const language = searchParams.get("lang");
const slug = searchParams.get("slug");
const language = req.query.lang as string;
const slug = req.query.slug as string;

const {
data: { conference },
Expand All @@ -47,26 +23,9 @@ export const handler = async (req: NextRequest) => {
const title = talk.title;
const speakers = talk.speakers.map((speaker) => speaker.fullName).join(", ");

return new ImageResponse(
await sendSocialCard(
res,
<TitleSubtitleCard title={title} subtitle={speakers} tag="keynote" />,
{
width: 1200,
height: 630,
fonts: [
{
name: "GeneralSans",
data: regularFontData,
style: "normal",
weight: 500,
},
{
name: "GeneralSans",
data: semiBoldFontData,
style: "normal",
weight: 600,
},
],
},
);
};

Expand Down
73 changes: 14 additions & 59 deletions frontend/src/pages/api/[lang]/news/[slug]/social-card.tsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,30 @@
import { ImageResponse } from "@vercel/og";

import type { NextRequest } from "next/server";
import type { NextApiRequest, NextApiResponse } from "next";

import { createClient } from "~/apollo/create-client";
import { TitleSubtitleCard } from "~/components/social-card-images/title-subtitle-card";
import { sendSocialCard } from "~/helpers/social-card";
import { queryNewsArticle } from "~/types";

export const config = {
runtime: "edge",
unstable_allowDynamic: ["/node_modules/.pnpm/**"],
};

const handler = async (req: NextRequest) => {
const regularFont = fetch(
new URL(
"./social-card-font/GeneralSans-Regular.otf",
req.url.substring(0, req.url.lastIndexOf("/api")),
),
).then((res) => res.arrayBuffer());
const semiBoldFont = fetch(
new URL(
"./social-card-font/GeneralSans-Semibold.otf",
req.url.substring(0, req.url.lastIndexOf("/api")),
),
).then((res) => res.arrayBuffer());

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const client = createClient();
const { searchParams } = new URL(req.url);

const language = searchParams.get("lang");
const slug = searchParams.get("slug");
const language = req.query.lang as string;
const slug = req.query.slug as string;

const [
regularFontData,
semiBoldFontData,
{
data: { newsArticle },
},
] = await Promise.all([
regularFont,
semiBoldFont,
queryNewsArticle(client, {
slug,
language,
hostname: process.env.cmsHostname,
}),
]);
const {
data: { newsArticle },
} = await queryNewsArticle(client, {
slug,
language,
hostname: process.env.cmsHostname,
});

const title = newsArticle?.title;
const excerpt = newsArticle?.excerpt;

return new ImageResponse(
await sendSocialCard(
res,
<TitleSubtitleCard title={title} subtitle={excerpt} />,
{
width: 1200,
height: 630,
fonts: [
{
name: "GeneralSans",
data: regularFontData,
style: "normal",
weight: 500,
},
{
name: "GeneralSans",
data: semiBoldFontData,
style: "normal",
weight: 600,
},
],
},
);
};

Expand Down
Loading
Loading