Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use lambda for og export api #98

Merged
merged 6 commits into from
Aug 31, 2024
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
2 changes: 1 addition & 1 deletion apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@ogstudio/auth": "workspace:*",
"@ogstudio/db": "workspace:*",
"@radix-ui/themes": "^3.0.3",
"@resvg/resvg-wasm": "2.4.0",
"@resvg/resvg-wasm": "2.6.2",
"@tanstack/react-query": "^5.35.1",
"@vercel/analytics": "^1.2.2",
"@vercel/kv": "^1.0.1",
Expand Down
9 changes: 0 additions & 9 deletions apps/dashboard/src/app/api/og/[key]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { kv } from "@vercel/kv";
import { initWasm } from "@resvg/resvg-wasm";
import type { NextRequest } from "next/server";
import type { OGElement } from "../../../../lib/types";
import {
Expand All @@ -8,13 +7,6 @@ import {
exportToSvg,
} from "../../../../lib/export";
import { loadFonts } from "../../../../lib/fonts";
// @ts-expect-error -- this file does exist
import resvgWasm from "../resvg.wasm?module";

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- wrong type
const initWasmPromise = initWasm(resvgWasm);

export const runtime = "edge";

export async function GET(
request: NextRequest,
Expand All @@ -32,7 +24,6 @@ export async function GET(
);

const reactElements = elementsToReactElements(ogElements, dynamicTexts);
await initWasmPromise;
const fonts = await loadFonts(ogElements);
const svg = await exportToSvg(reactElements, fonts);
const png = await exportToPng(svg);
Expand Down
9 changes: 0 additions & 9 deletions apps/dashboard/src/app/api/og/template/[name]/route.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { initWasm } from "@resvg/resvg-wasm";
import {
elementsToReactElements,
exportToPng,
exportToSvg,
} from "../../../../../lib/export";
import { TEMPLATES } from "../../../../../lib/templates";
// @ts-expect-error -- this file does exist
import resvgWasm from "../../resvg.wasm?module";
import { loadFonts } from "../../../../../lib/fonts";

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- wrong type
const initWasmPromise = initWasm(resvgWasm);

export const runtime = "edge";

export async function GET(
_: Request,
{ params }: { params: { name: string } },
Expand All @@ -29,7 +21,6 @@ export async function GET(

const ogElements = template.elements;
const reactElements = elementsToReactElements(ogElements);
await initWasmPromise;
const fonts = await loadFonts(ogElements);
const svg = await exportToSvg(reactElements, fonts);
const png = await exportToPng(svg);
Expand Down
7 changes: 3 additions & 4 deletions apps/dashboard/src/lib/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ if (process.env.VITEST_POOL_ID) {
"node_modules/@resvg/resvg-wasm/index_bg.wasm",
),
);
// @ts-expect-error -- only present in Edge Runtime
} else if (typeof EdgeRuntime !== "string") {
} else {
initWasmPromise = initWasm(
fetch("https://unpkg.com/@resvg/resvg-wasm@2.4.0/index_bg.wasm", {
fetch("https://unpkg.com/@resvg/resvg-wasm@2.6.2/index_bg.wasm", {
cache: "no-store",
}),
);
Expand Down Expand Up @@ -130,7 +129,7 @@ export async function exportToSvg(
}

/**
* Export an SVG string to a PNG Uint8Array, using the provided font buffers.
* Export an SVG string to a PNG Uint8Array.
*/
export async function exportToPng(svg: string): Promise<Uint8Array> {
await initWasmPromise;
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/src/lib/fonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export async function loadFonts(elements: OGElement[]): Promise<FontData[]> {
const data = await fetch(
// @ts-expect-error -- wrong inference
`https://fonts.bunny.net/${fontName}/files/${fontName}-latin-${element.fontWeight}-normal.woff`,
{ cache: "no-store" },
).then((response) => response.arrayBuffer());

const fontData: FontData = {
Expand Down
14 changes: 9 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading