Skip to content

Commit

Permalink
Fix opengraph image
Browse files Browse the repository at this point in the history
  • Loading branch information
nsaunders committed Jul 30, 2023
1 parent 8a10701 commit 65b8517
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** @type {import("next").NextConfig} */
const nextConfig = {
...(process.env.DYNAMIC ? undefined : { output: "export" }),
...(process.env.VERCEL_ENV ? undefined : { output: "export" }),
images: {
unoptimized: !process.env.DYNAMIC,
unoptimized: !process.env.VERCEL_ENV,
remotePatterns: [
{ protocol: "https", hostname: "media.githubusercontent.com" },
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "ts-node -P tsconfig.scripts.json src/scripts/rss && next build",
"build": "cp node_modules/@fontsource/lato/files/lato-latin-400-normal.woff public && ts-node -P tsconfig.scripts.json src/scripts/rss && next build",
"start": "next start",
"lint": "next lint"
},
Expand Down
Binary file added public/lato-latin-400-normal.woff
Binary file not shown.
27 changes: 23 additions & 4 deletions src/app/posts/[name]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ImageResponse } from "next/server";
import fs from "fs/promises";
import * as Post from "@/data/Post";
import fs from "fs/promises";
import http from "http";

// Route segment config
export const runtime = "nodejs";
Expand All @@ -13,6 +14,26 @@ export const size = {

export const contentType = "image/png";

async function getFont(): Promise<Buffer> {
const url = process.env.APP_URL;
if (!url) {
return await fs.readFile(
"node_modules/@fontsource/lato/files/lato-latin-400-normal.woff"
);
}
return new Promise((resolve, reject) => {
http
.get(`${url}/fonts/lato-latin-400-normal.woff`, (res) => {
const chunks: any[] = [];
res.on("data", (c) => chunks.push(c));
res.on("end", () => resolve(Buffer.concat(chunks)));
})
.on("error", (err) => {
reject(err);
});
});
}

export default async function Image({
params: { name },
}: {
Expand Down Expand Up @@ -68,9 +89,7 @@ export default async function Image({
fonts: [
{
name: "Lato",
data: await fs.readFile(
"node_modules/@fontsource/lato/files/lato-latin-400-normal.woff"
),
data: await getFont(),
weight: 400,
style: "normal",
},
Expand Down

0 comments on commit 65b8517

Please sign in to comment.