diff --git a/frontend/src/helpers/social-card.tsx b/frontend/src/helpers/social-card.tsx
index 05928f061f..a30c47f0dd 100644
--- a/frontend/src/helpers/social-card.tsx
+++ b/frontend/src/helpers/social-card.tsx
@@ -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) => {
@@ -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);
+};
diff --git a/frontend/src/pages/api/[lang]/event/[slug]/social-card.tsx b/frontend/src/pages/api/[lang]/event/[slug]/social-card.tsx
index 9d378ea7f6..602a8a871a 100644
--- a/frontend/src/pages/api/[lang]/event/[slug]/social-card.tsx
+++ b/frontend/src/pages/api/[lang]/event/[slug]/social-card.tsx
@@ -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,
,
- {
- width: 1200,
- height: 630,
- fonts: [
- {
- name: "GeneralSans",
- data: regularFontData,
- style: "normal",
- weight: 500,
- },
- {
- name: "GeneralSans",
- data: semiBoldFontData,
- style: "normal",
- weight: 600,
- },
- ],
- },
);
};
diff --git a/frontend/src/pages/api/[lang]/keynotes/[slug]/social-card.tsx b/frontend/src/pages/api/[lang]/keynotes/[slug]/social-card.tsx
index 6402b61316..f36efaef1b 100644
--- a/frontend/src/pages/api/[lang]/keynotes/[slug]/social-card.tsx
+++ b/frontend/src/pages/api/[lang]/keynotes/[slug]/social-card.tsx
@@ -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 },
@@ -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,
,
- {
- width: 1200,
- height: 630,
- fonts: [
- {
- name: "GeneralSans",
- data: regularFontData,
- style: "normal",
- weight: 500,
- },
- {
- name: "GeneralSans",
- data: semiBoldFontData,
- style: "normal",
- weight: 600,
- },
- ],
- },
);
};
diff --git a/frontend/src/pages/api/[lang]/news/[slug]/social-card.tsx b/frontend/src/pages/api/[lang]/news/[slug]/social-card.tsx
index 392208dff1..b0563e6de6 100644
--- a/frontend/src/pages/api/[lang]/news/[slug]/social-card.tsx
+++ b/frontend/src/pages/api/[lang]/news/[slug]/social-card.tsx
@@ -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,
,
- {
- width: 1200,
- height: 630,
- fonts: [
- {
- name: "GeneralSans",
- data: regularFontData,
- style: "normal",
- weight: 500,
- },
- {
- name: "GeneralSans",
- data: semiBoldFontData,
- style: "normal",
- weight: 600,
- },
- ],
- },
);
};
diff --git a/frontend/src/pages/api/[lang]/social-card.tsx b/frontend/src/pages/api/[lang]/social-card.tsx
index b317ec7e32..58568be83e 100644
--- a/frontend/src/pages/api/[lang]/social-card.tsx
+++ b/frontend/src/pages/api/[lang]/social-card.tsx
@@ -1,17 +1,14 @@
-import { ImageResponse } from "@vercel/og";
+import { promises as fs } from "fs";
+import path from "path";
-import type { NextRequest } from "next/server";
+import type { NextApiRequest, NextApiResponse } from "next";
import { createClient } from "~/apollo/create-client";
import { Logo } from "~/components/logo";
+import { sendSocialCard } from "~/helpers/social-card";
import type { Language } from "~/locale/languages";
import { querySocialCard } from "~/types";
-export const config = {
- runtime: "edge",
- unstable_allowDynamic: ["/node_modules/.pnpm/**"],
-};
-
const getDays = ({ start, end }: { start: string; end: string }) => {
// assuming the same month
const startDate = new Date(start);
@@ -36,37 +33,21 @@ const getYear = ({ end }: { end: string }) => {
return endDate.getFullYear();
};
-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 mainIllustration = fetch(
- new URL(
- "./images/main-illustration.png",
- req.url.substring(0, req.url.lastIndexOf("/api")),
- ),
- ).then((res) => res.arrayBuffer());
-
- const [regularFontData, semiBoldFontData, mainIllustrationData] =
- await Promise.all([regularFont, semiBoldFont, mainIllustration]);
+const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const client = createClient();
- const { data } = await querySocialCard(client, {
- code: process.env.conferenceCode,
- });
- const { searchParams } = new URL(req.url);
+ const [mainIllustration, { data }] = await Promise.all([
+ fs.readFile(
+ path.join(process.cwd(), "public/images/main-illustration.png"),
+ ),
+ querySocialCard(client, {
+ code: process.env.conferenceCode,
+ }),
+ ]);
- const language = searchParams.get("lang") as Language;
+ const language = req.query.lang as Language;
- return new ImageResponse(
+ await sendSocialCard(
+ res,
{
{
,
- {
- width: 1200,
- height: 630,
- fonts: [
- {
- name: "GeneralSans",
- data: regularFontData,
- style: "normal",
- weight: 500,
- },
- {
- name: "GeneralSans",
- data: semiBoldFontData,
- style: "normal",
- weight: 600,
- },
- ],
- },
);
};