Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gurubu-client): open graph image generator added #122

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Binary file added gurubu-client/public/logo-with-text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions gurubu-client/src/app/api/og/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ImageResponse } from "next/og";
import { CSSProperties } from "react";

export const runtime = "edge";

const imageStyle: CSSProperties = {
backgroundColor: "#ffffff",
backgroundSize: "150px 150px",
height: "100%",
width: "100%",
display: "flex",
textAlign: "center",
alignItems: "center",
justifyContent: "center",
flexDirection: "column",
flexWrap: "nowrap",
fontSize: "48px",
};

const imageSize = {
width: 1200,
height: 630,
};

export async function GET(request: Request) {
try {
const imageData = await fetch(
new URL("../../../../public/logo-with-text.png", import.meta.url)
).then((res) => res.arrayBuffer());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to take it as an ordinary picture?

Copy link
Contributor Author

@hanifisenturk hanifisenturk Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mertcanaltin actually I thought that. As I used the edge runtime for this operation, all resources have to get with fetch api. This link explains that as well. Of course, if I used Node runtime, we can get the data via fs.readFile.


return new ImageResponse(
(
<div style={imageStyle}>
{imageData && (
<img width={372} height={90} src={imageData as unknown as string} />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we could make this place cleaner.

src={imageData as unknown as string}

)}
Simple, fast and practical grooming.
</div>
),
imageSize
);
} catch (e: any) {
return new ImageResponse(
(
<div style={imageStyle}>
<h1>GuruBu</h1>
Simple, fast and practical grooming.
</div>
),
imageSize
);
}
}
10 changes: 10 additions & 0 deletions gurubu-client/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ const spaceGrotesk = Space_Grotesk({ subsets: ["latin"], variable: '--font-space
export const metadata: Metadata = {
title: "Gurubu | Simple, fast and practical grooming",
description: "Simple, fast and practical grooming.",
openGraph: {
images: [
{
url: "/api/og",
width: 1200,
height: 630,
alt: "Simple, fast and practical grooming.",
},
],
},
};

export default function RootLayout({
Expand Down
9 changes: 9 additions & 0 deletions gurubu-client/src/app/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MetadataRoute } from "next";

export default function robots(): MetadataRoute.Robots {
return {
rules: {
allow: ["/", "/api/og/*"],
},
};
}