-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opengraph): generated fallback image
- Loading branch information
Showing
8 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getBackground } from "./background"; | ||
|
||
interface OpenGraphImageProps { | ||
title: string; | ||
description?: string; | ||
} | ||
|
||
const OpenGraphImage = ({ title, description }: OpenGraphImageProps) => { | ||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
color: "white", | ||
background: getBackground(), | ||
width: "100%", | ||
height: "100%", | ||
padding: "0 75px", | ||
}} | ||
> | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: "1rem", | ||
height: "70%", | ||
marginTop: "125px", | ||
}} | ||
> | ||
<span | ||
style={{ | ||
fontFamily: "Recoleta", | ||
fontSize: 78, | ||
}} | ||
> | ||
{title} | ||
</span> | ||
{description && ( | ||
<span | ||
style={{ | ||
fontFamily: "Graphik", | ||
fontWeight: 400, | ||
fontSize: 36, | ||
}} | ||
> | ||
{description.length > 160 | ||
? description.substring(0, 160) + "[...]" | ||
: description} | ||
</span> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default OpenGraphImage; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { readFile } from "fs/promises"; | ||
import { join } from "path"; | ||
|
||
type Weight = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900; | ||
type FontStyle = "normal" | "italic"; | ||
interface FontOptions { | ||
data: Buffer | ArrayBuffer; | ||
name: string; | ||
weight?: Weight; | ||
style?: FontStyle; | ||
lang?: string; | ||
} | ||
|
||
function readFont(filename: string) { | ||
return readFile(join(process.cwd(), "public/fonts", filename)); | ||
} | ||
|
||
export async function getFonts(): Promise<FontOptions[]> { | ||
return [ | ||
{ | ||
data: await readFont("graphik_regular.woff"), | ||
name: "Graphik", | ||
weight: 400, | ||
style: "normal", | ||
}, | ||
{ | ||
data: await readFont("graphik_medium.woff"), | ||
name: "Graphik", | ||
weight: 600, | ||
style: "normal", | ||
}, | ||
{ | ||
data: await readFont("recoleta.ttf"), | ||
name: "Recoleta", | ||
weight: 600, | ||
style: "normal", | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ImageResponse } from "next/og"; | ||
import { NextRequest } from "next/server"; | ||
import OpenGraphImage from "./OpenGraphImage"; | ||
import { getFonts } from "./fontUtils"; | ||
|
||
export async function GET(request: NextRequest) { | ||
const searchParams = request.nextUrl.searchParams; | ||
const title = searchParams.get("title") ?? "Variant"; | ||
const description = searchParams.get("description"); | ||
return new ImageResponse( | ||
<OpenGraphImage title={title} description={description ?? undefined} />, | ||
{ | ||
width: 1200, | ||
height: 630, | ||
fonts: await getFonts(), | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters