Skip to content

Commit

Permalink
docs(OpenGraphImage): document open graph image customization
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 6, 2024
1 parent 18e9578 commit ef6bca3
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,75 @@ export default MyCustomComponent;

By using fetchWithToken, you ensure that all data fetching happens securely, with the server-side API route handling the sensitive token.

### OpenGraph image customization

#### Custom fonts

The following font utils file can be defined:

> fonts must be placed in `/public/fonts`
```tsx
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",
},
];
}
```

followed by a modification of the image route response:

```tsx
(<OpenGraphImage title={title} description={description ?? undefined} />),
{
width: 1200,
height: 630,
fonts: await getFonts(), // add this line
};
```

#### Custom background

Simply use the CSS background property on the root element with a base64-encoded data url

```css
background: url("data:image/png;base64,...");
```

### Troubleshooting

- Sanity Preview: While the Sanity preview functionality is not fully optimized, it currently meets the essential requirements.
Expand Down

0 comments on commit ef6bca3

Please sign in to comment.