Next.js Discord

Discord Forum

Next OpenGraph Image

Unanswered
Himalayan posted this in #help-forum
Open in Discord
HimalayanOP
Hello. I am following docs with putting opengraph-image to root component and still it is not working. I even used metadata with OpenGraph Image and put local host address here and still the image didnt show up. Can anyone tell me how to do it? Thanks a lot

7 Replies

Cape lion
I haven't tried with the image files, but with the route handlers to generate the OG image as they have done here https://github.com/vercel/next-app-router-playground. Link to repo which I have implemented: https://github.com/dorji-dev/next_app/tree/main/src/app. In short, you create a route handler, in my case api/og/route.tsx and from that return the generated ImageResponse. Now from your layout you do this:
export const metadata: Metadata = {
  title: {
    default: "Next.js App Router",
    template: "%s | Next.js App Router",
  },
  metadataBase: new URL("https://next-app-gamma-lovat.vercel.app"),
  openGraph: {
    title: "Everyday features with Next.js App Router",
    description:
      "A demo of every day features and concepts using NextJs app router",
    images: ["/api/og"],
  },
};
, where you set the route handler path as images src. After hosting, you need to set your domain as metadataBase value like in above.
@Himalayan Hello. I am following docs with putting opengraph-image to root component and still it is not working. I even used metadata with OpenGraph Image and put local host address here and still the image didnt show up. Can anyone tell me how to do it? Thanks a lot
"Localhost address here"?

Thats not how it works. To test the OG you'll have to host the app somewhere or use some kind of opengraph tester directly on your pc. Online platforms wont work since they obviously dont know what localhost is and how to connect to it.
HimalayanOP
So basically just set up metadataBase: new URL("https://next-app-gamma-lovat.vercel.app"), for my domain. I am using cloudflare currently and every time i refresh servers to upload changes the url address will be changed. So the best idea is to buy domain and then set metadataBase to my domain address
Cape lion
I use this https://og.nullnull.dev/ to test OG image locally. Has a chrome extension and can verify it's great to be able to test locally
HimalayanOP
Okay thanks a lot bro, appreciate it
Cape lion
After hosting you can test it here: https://www.opengraph.xyz/