perf: og image caching v2 (#23189)

This commit is contained in:
Benny Joo authored and GitHub committed 2025-10-29 10:36:17 -03:00
1 parent 8ba65c4450
commit 14e845ea1f
8 files changed
+251 -99

No files matched your search

@@ -1,6 +1,8 @@
import { NextRequest } from "next/server";
import { describe, expect, test, vi, beforeEach } from "vitest";
import { getOGImageVersion } from "@calcom/lib/OgImages";
import { GET } from "../route";
vi.mock("next/og", () => ({
@@ -14,14 +16,19 @@ vi.mock("next/og", () => ({
})),
}));
vi.mock("@calcom/lib/OgImages", () => ({
Meeting: vi.fn(() => null),
App: vi.fn(() => null),
Generic: vi.fn(() => null),
}));
vi.mock("@calcom/lib/OgImages", async (importOriginal) => {
return await importOriginal();
});
vi.mock("@calcom/lib/constants", () => ({
WEBAPP_URL: "http://localhost:3000",
vi.mock(import("@calcom/lib/constants"), async (importOriginal) => {
return await importOriginal();
});
vi.mock("@calcom/web/public/app-store/svg-hashes.json", () => ({
default: {
huddle01video: "81a0653b",
zoomvideo: "d1c78abf",
},
}));
global.fetch = vi.fn();
@@ -82,7 +89,7 @@ describe("GET /api/social/og/image", () => {
const response = await GET(request);
expect(response.status).toBe(404);
expect(await response.text()).toBe("What you're looking for is not here..");
expect(await response.text()).toBe("Wrong image type");
});
test("returns 404 when invalid type parameter is provided", async () => {
@@ -90,7 +97,7 @@ describe("GET /api/social/og/image", () => {
const response = await GET(request);
expect(response.status).toBe(404);
expect(await response.text()).toBe("What you're looking for is not here..");
expect(await response.text()).toBe("Wrong image type");
});
});
@@ -107,4 +114,33 @@ describe("GET /api/social/og/image", () => {
expect(await response.text()).toBe("Internal server error");
});
});
describe("getOGImageVersion with SVG hash", () => {
test("app type: ETag changes when SVG hash is provided", async () => {
const etagWithoutHash = await getOGImageVersion("app");
const etagWithHash = await getOGImageVersion("app", {
slug: "huddle01video",
svgHash: "81a0653b",
});
expect(etagWithoutHash).toBeTruthy();
expect(etagWithHash).toBeTruthy();
expect(etagWithHash).not.toBe(etagWithoutHash);
});
test("app type: different SVG hashes produce different ETags", async () => {
const etagHash1 = await getOGImageVersion("app", {
slug: "huddle01video",
svgHash: "81a0653b",
});
const etagHash2 = await getOGImageVersion("app", {
slug: "zoomvideo",
svgHash: "d1c78abf",
});
expect(etagHash1).toBeTruthy();
expect(etagHash2).toBeTruthy();
expect(etagHash1).not.toBe(etagHash2);
});
});
});
+28 -8
View File
@@ -3,7 +3,7 @@ import type { NextRequest } from "next/server";
import type { SatoriOptions } from "satori";
import { z, ZodError } from "zod";
import { Meeting, App, Generic } from "@calcom/lib/OgImages";
import { Meeting, App, Generic, getOGImageVersion } from "@calcom/lib/OgImages";
import { WEBAPP_URL } from "@calcom/lib/constants";
export const runtime = "edge";
@@ -22,6 +22,7 @@ const appSchema = z.object({
name: z.string(),
description: z.string(),
slug: z.string(),
logoUrl: z.string(),
});
const genericSchema = z.object({
@@ -74,6 +75,7 @@ async function handler(req: NextRequest) {
imageType,
});
const etag = await getOGImageVersion("meeting");
const img = new ImageResponse(
(
<Meeting
@@ -89,7 +91,9 @@ async function handler(req: NextRequest) {
status: 200,
headers: {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
"Cache-Control":
"public, max-age=31536000, immutable, s-maxage=31536000, stale-while-revalidate=31536000",
ETag: `"${etag}"`,
},
});
} catch (error) {
@@ -111,19 +115,32 @@ async function handler(req: NextRequest) {
}
case "app": {
try {
const { name, description, slug } = appSchema.parse({
const { name, description, slug, logoUrl } = appSchema.parse({
name: searchParams.get("name"),
description: searchParams.get("description"),
slug: searchParams.get("slug"),
logoUrl: searchParams.get("logoUrl"),
imageType,
});
const img = new ImageResponse(<App name={name} description={description} slug={slug} />, ogConfig);
// Get SVG hash for the app
const svgHashesModule = await import("@calcom/web/public/app-store/svg-hashes.json");
const SVG_HASHES = svgHashesModule.default ?? {};
const svgHash = SVG_HASHES[slug] ?? undefined;
const etag = await getOGImageVersion("app", { svgHash });
const img = new ImageResponse(
<App name={name} description={description} slug={slug} logoUrl={logoUrl} />,
ogConfig
);
return new Response(img.body, {
status: 200,
headers: {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
"Cache-Control":
"public, max-age=31536000, immutable, s-maxage=31536000, stale-while-revalidate=31536000",
ETag: `"${etag}"`,
},
});
} catch (error) {
@@ -151,13 +168,16 @@ async function handler(req: NextRequest) {
imageType,
});
const etag = await getOGImageVersion("generic");
const img = new ImageResponse(<Generic title={title} description={description} />, ogConfig);
return new Response(img.body, {
status: 200,
headers: {
"Content-Type": "image/png",
"Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
"Cache-Control":
"public, max-age=31536000, immutable, s-maxage=31536000, stale-while-revalidate=31536000",
ETag: `"${etag}"`,
},
});
} catch (error) {
@@ -178,9 +198,9 @@ async function handler(req: NextRequest) {
}
default:
return new Response("What you're looking for is not here..", { status: 404 });
return new Response("Wrong image type", { status: 404 });
}
} catch (error) {
} catch {
return new Response("Internal server error", { status: 500 });
}
}
+4
View File
@@ -0,0 +1,4 @@
declare module "@calcom/web/public/app-store/svg-hashes.json" {
const value: Record<string, string>;
export default value;
}