Skip to content

Commit 0705f90

Browse files
committed
feat: ✨ add og api
1 parent 71106d6 commit 0705f90

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed

src/app/[locale]/layout.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import Footer from "~/components/layout/footer";
33
import Header from "~/components/layout/header";
44
import ThemeProvider from "~/components/shared/theme-provider";
55
import { Toaster } from "~/components/ui/toaster";
6-
import { siteConfig } from "~/config/site";
6+
import { siteConfig, siteUrl } from "~/config/site";
77
import { I18nProviderClient } from "~/locales/client";
8+
import { getScopedI18n } from "~/locales/server";
89

910
type Props = {
1011
params: { locale: string };
1112
searchParams: { [key: string]: string | string[] | undefined };
1213
};
1314

1415
export async function generateMetadata({ params }: Props): Promise<Metadata> {
15-
const locale = params.locale || "en";
16+
const locale = params.locale;
1617
const site = siteConfig(locale);
1718

19+
const t = await getScopedI18n("hero");
20+
const title = t("main");
21+
const siteOgImage = `${siteUrl}/api/og?title=${title}`;
22+
1823
return {
1924
title: {
2025
default: site.name,
@@ -42,14 +47,14 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
4247
creator: "moinulmoin",
4348
openGraph: {
4449
type: "website",
45-
locale: "en_US",
50+
locale: locale,
4651
url: site.url,
4752
title: site.name,
4853
description: site.description,
4954
siteName: site.name,
5055
images: [
5156
{
52-
url: site.ogImage,
57+
url: siteOgImage,
5358
width: 1200,
5459
height: 630,
5560
alt: site.name,
@@ -60,7 +65,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
6065
card: "summary_large_image",
6166
title: site.name,
6267
description: site.description,
63-
images: [site.ogImage],
68+
images: [siteOgImage],
6469
creator: "@immoinulmoin",
6570
},
6671
icons: {

src/app/api/og/route.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ImageResponse } from "next/og";
2+
import { RenderIMGEl } from "~/components/OGImgEl";
3+
import Logo from "../../../../public/chad-next.png";
4+
5+
export const runtime = "edge";
6+
7+
export async function GET(request: Request) {
8+
const { searchParams } = new URL(request.url);
9+
const hasTitle = searchParams.has("title");
10+
const title = hasTitle
11+
? searchParams.get("title")
12+
: "Quick Starter Template for your Next project";
13+
14+
try {
15+
return new ImageResponse(
16+
RenderIMGEl({
17+
logo: process.env.NEXT_PUBLIC_APP_URL + Logo.src,
18+
title: title as string,
19+
}),
20+
{
21+
width: 1200,
22+
height: 630,
23+
}
24+
);
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
} catch (e: any) {
27+
console.log(`${e.message}`);
28+
return new Response(`Failed to generate the image`, {
29+
status: 500,
30+
});
31+
}
32+
}

src/app/[locale]/opengraph-image.tsx renamed to src/app/opengraph-image.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @next/next/no-img-element */
22
import { ImageResponse } from "next/og";
33
import { getScopedI18n } from "~/locales/server";
4-
import Logo from "../../../public/chad-next.png";
4+
import Logo from "../../public/chad-next.png";
55

66
export const runtime = "edge";
77
export const contentType = "image/png";

src/components/OGImgEl.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* eslint-disable @next/next/no-img-element */
2+
export const RenderIMGEl = ({
3+
logo,
4+
title,
5+
}: {
6+
logo: string;
7+
title: string;
8+
}) => {
9+
return (
10+
<div
11+
style={{
12+
backgroundImage:
13+
"linear-gradient(to bottom right, #E0E7FF 25%, #ffffff 50%, #CFFAFE 75%)",
14+
}}
15+
tw="h-full w-full flex flex-col items-center justify-center bg-white"
16+
>
17+
<img
18+
src={logo}
19+
alt="ChadNext Logo"
20+
tw="w-20 h-20 mb-4 opacity-95"
21+
width={80}
22+
height={80}
23+
/>
24+
<h1
25+
style={{
26+
fontSize: "80px",
27+
fontWeight: 900,
28+
background:
29+
"linear-gradient(to bottom right, #000000 21.66%, #78716c 86.47%)",
30+
backgroundClip: "text",
31+
color: "transparent",
32+
lineHeight: "5rem",
33+
letterSpacing: "-0.02em",
34+
}}
35+
>
36+
ChadNext
37+
</h1>
38+
<h2
39+
style={{
40+
fontSize: "40px",
41+
fontWeight: 700,
42+
lineHeight: "5rem",
43+
letterSpacing: "-0.02em",
44+
}}
45+
>
46+
{title}
47+
</h2>
48+
</div>
49+
);
50+
};

0 commit comments

Comments
 (0)