Skip to content

Commit 9b5690f

Browse files
committed
feat: custom fonts example
1 parent 0bc117a commit 9b5690f

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

.changeset/grumpy-moose-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"framesjs-starter": patch
3+
---
4+
5+
feat: custom fonts example
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* eslint-disable react/jsx-key */
2+
import { Button } from "frames.js/next";
3+
import { frames } from "../frames";
4+
5+
// without this line, this type of importing fonts doesn't work for some reason
6+
export const runtime = "edge";
7+
8+
const interRegularFont = fetch(
9+
new URL("/public/Inter-Regular.ttf", import.meta.url)
10+
).then((res) => res.arrayBuffer());
11+
const interBoldFont = fetch(
12+
new URL("/public/Inter-Bold.ttf", import.meta.url)
13+
).then((res) => res.arrayBuffer());
14+
const firaScriptFont = fetch(
15+
new URL("/public/FiraCodeiScript-Regular.ttf", import.meta.url)
16+
).then((res) => res.arrayBuffer());
17+
18+
const handleRequest = frames(async (ctx) => {
19+
const [interRegularFontData, interBoldFontData, firaScriptFontData] =
20+
await Promise.all([interRegularFont, interBoldFont, firaScriptFont]);
21+
22+
return {
23+
image: (
24+
<span tw="flex flex-col">
25+
<div>Edge functions example custom fonts</div>
26+
<div style={{ marginTop: 40, fontWeight: 400 }}>Regular Inter Font</div>
27+
<div style={{ marginTop: 40, fontWeight: 700 }}>Bold Inter Font</div>
28+
<div
29+
style={{
30+
fontFamily: "'Fira Code', monospace",
31+
marginTop: 40,
32+
}}
33+
>
34+
Fira
35+
</div>
36+
</span>
37+
),
38+
buttons: [
39+
<Button action="post" target={"/nodejs"}>
40+
Node.js
41+
</Button>,
42+
],
43+
imageOptions: {
44+
fonts: [
45+
{
46+
name: "Inter",
47+
data: interRegularFontData,
48+
weight: 400,
49+
},
50+
{
51+
name: "Inter",
52+
data: interBoldFontData,
53+
weight: 700,
54+
},
55+
{
56+
name: "Fira Code",
57+
data: firaScriptFontData,
58+
weight: 700,
59+
},
60+
],
61+
},
62+
};
63+
});
64+
65+
export const POST = handleRequest;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createFrames } from "frames.js/next";
2+
3+
export const frames = createFrames({
4+
basePath: "/examples/new-api-custom-font/frames",
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* eslint-disable react/jsx-key */
2+
import { Button } from "frames.js/next";
3+
import * as fs from "node:fs/promises";
4+
import * as path from "node:path";
5+
import { frames } from "../frames";
6+
7+
export const runtime = "nodejs";
8+
9+
const interRegularFont = fs.readFile(
10+
path.join(path.resolve(process.cwd(), "public"), "Inter-Regular.ttf")
11+
);
12+
13+
const interBoldFont = fs.readFile(
14+
path.join(path.resolve(process.cwd(), "public"), "Inter-Bold.ttf")
15+
);
16+
17+
const firaScriptFont = fs.readFile(
18+
path.join(
19+
path.resolve(process.cwd(), "public"),
20+
"FiraCodeiScript-Regular.ttf"
21+
)
22+
);
23+
24+
const handleRequest = frames(async (ctx) => {
25+
const [interRegularFontData, interBoldFontData, firaScriptData] =
26+
await Promise.all([interRegularFont, interBoldFont, firaScriptFont]);
27+
28+
return {
29+
buttons: [
30+
<Button target={"/edge"} action="post">
31+
Edge Fn
32+
</Button>,
33+
],
34+
image: (
35+
<span tw="flex flex-col">
36+
<div>Node.js example custom fonts</div>
37+
<div style={{ marginTop: 40, fontWeight: 400 }}>Regular Inter Font</div>
38+
<div style={{ marginTop: 40, fontWeight: 700 }}>Bold Inter Font</div>
39+
<div
40+
style={{
41+
fontFamily: "'Fira Code', monospace",
42+
marginTop: 40,
43+
}}
44+
>
45+
Fira
46+
</div>
47+
</span>
48+
),
49+
imageOptions: {
50+
fonts: [
51+
{
52+
name: "Inter",
53+
data: interRegularFontData,
54+
weight: 400,
55+
},
56+
{
57+
name: "Inter",
58+
data: interBoldFontData,
59+
weight: 700,
60+
},
61+
{
62+
name: "Fira Code",
63+
data: firaScriptData,
64+
weight: 700,
65+
},
66+
],
67+
},
68+
};
69+
});
70+
71+
export const POST = handleRequest;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable react/jsx-key */
2+
import { Button } from "frames.js/next";
3+
import { frames } from "./frames";
4+
5+
const handler = frames(async (ctx) => {
6+
return {
7+
image: <div tw="flex">Custom fonts example</div>,
8+
buttons: [
9+
<Button action="post" target="/nodejs">
10+
Node.js runtime
11+
</Button>,
12+
<Button action="post" target="/edge">
13+
Edge function
14+
</Button>,
15+
],
16+
};
17+
});
18+
19+
export const GET = handler;
20+
export const POST = handler;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Link from "next/link";
2+
import { currentURL, vercelURL } from "../../utils";
3+
import { createDebugUrl } from "../../debug";
4+
import type { Metadata } from "next";
5+
import { fetchMetadata } from "frames.js/next";
6+
7+
export async function generateMetadata(): Promise<Metadata> {
8+
return {
9+
title: "New api example",
10+
description: "This is a new api example",
11+
other: {
12+
...(await fetchMetadata(
13+
new URL(
14+
"/examples/new-api-custom-font/frames",
15+
vercelURL() || "http://localhost:3000"
16+
)
17+
)),
18+
},
19+
};
20+
}
21+
22+
export default async function Home() {
23+
const url = currentURL("/examples/new-api-custom-font");
24+
25+
return (
26+
<div>
27+
Custom font example{" "}
28+
<Link href={createDebugUrl(url)} className="underline">
29+
Debug
30+
</Link>
31+
</div>
32+
);
33+
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)