OG:image not previewing in async function generateMetada()?
Answered
Pteromalid wasp posted this in #help-forum
Pteromalid waspOP
Hello! I'm using the generateMetadata inside a
I have no errors in development, but when I deploy my application to Vercel and I try to preview those metadata either copy/paste inside an email, iMessage, etc or using a website like these (https://socialsharepreview.com/) (https://www.opengraph.xyz/) I have 0 preview.
Here is a link to test (https://fondazione-santarelli.vercel.app/collection/work/kline-monument).
It's very strange because I only used Next.js documentation (https://nextjs.org/docs/app/api-reference/functions/generate-metadata).
Here is the
layout.tsx.I have no errors in development, but when I deploy my application to Vercel and I try to preview those metadata either copy/paste inside an email, iMessage, etc or using a website like these (https://socialsharepreview.com/) (https://www.opengraph.xyz/) I have 0 preview.
Here is a link to test (https://fondazione-santarelli.vercel.app/collection/work/kline-monument).
It's very strange because I only used Next.js documentation (https://nextjs.org/docs/app/api-reference/functions/generate-metadata).
Here is the
layout.tsx:import ...
type Props = {
params: {
slug: string;
locale: string;
};
children: ReactNode;
};
export async function generateMetadata({
params,
}: Props): Promise<Metadata | null> {
const locale = params.locale;
const workMetadata = await clientFetch<TYPEMeta>(METADATA_QUERY, {
slug: params.slug,
locale: locale,
next: { revalidate: 10 },
});
if (workMetadata === null) return null;
const metadata = workMetadata.meta;
const fallbackMetadata = workMetadata.fallbackMeta;
const metaTitle = `${metadata.title}${SEO_SEPARATOR}${fallbackMetadata.title}`;
const temporaryImage = workMetadata.image;
return {
title: metaTitle,
openGraph: {
title: metaTitle,
description:
metadata.description !== null
? metadata.description
: fallbackMetadata.description,
siteName: fallbackMetadata.title,
url: WEBSITE_BASE_URL,
locale: locale,
images: {
url: temporaryImage.url,
secureUrl: temporaryImage.url,
type: "image/png",
alt: metaTitle,
width: temporaryImage.width,
height: temporaryImage.height,
},
},
keywords:
metadata.keywords !== null
? metadata.keywords
: fallbackMetadata.keywords,
};
}
export default function Layout({ children }: Props) {
return <>{children}</>;
}Answered by Pteromalid wasp
I'm trying to understand, but I think it's beyond my knowledge. 😦
edit: By trying to make the small boilerplate as your demand, I remooved all the components and it's works. So I'll find out that the solution was to correct a
edit: By trying to make the small boilerplate as your demand, I remooved all the components and it's works. So I'll find out that the solution was to correct a
'use client' component that appears in the page that contain an error 🤣30 Replies
Pteromalid waspOP
Everything is my
<Head> at https://fondazione-santarelli.vercel.app/collection/work/kline-monument (for example) is well generated!@joulev do you use useSearchParams anywhere?
Pteromalid waspOP
Nop, absolutely not
@joulev if not, give me a minimal reproduction repository
Pteromalid waspOP
How can I quickly do that?
basically you create a nextjs repo, as minimal as possible (remove all your biz logic), but still reproducing the error above so i can clone and debug
@joulev <https://stackoverflow.com/help/minimal-reproducible-example>
Pteromalid waspOP
The problem is it works well on development
@joulev <https://stackoverflow.com/help/minimal-reproducible-example>
Pteromalid waspOP
Are you available for a quick call? So I can show around quickly maybe?
no, i'm not available for calls. you have to make something that i can use to debug
Pteromalid waspOP
I'll try asap!
Thanks
@joulev no, i'm not available for calls. you have to make something that i can use to debug
Pteromalid waspOP
When duplcating the projet, I got this:
metadata.metadataBase is not set for resolving social open graph or twitter images, fallbacks to "http://localhost:3000". See https://nextjs.org/docs/app/api-reference/functions/generate-metadata#metadatabase@Pteromalid wasp When duplcating the projet, I got this: `metadata.metadataBase is not set for resolving social open graph or twitter images, fallbacks to "http://localhost:3000". See https://nextjs.org/docs/app/api-reference/functions/generate-metadata#metadatabase`
It gave you a link, you can go read that link to see more
@joulev It gave you a link, you can go read that link to see more
Pteromalid waspOP
I'm trying to understand, but I think it's beyond my knowledge. 😦
edit: By trying to make the small boilerplate as your demand, I remooved all the components and it's works. So I'll find out that the solution was to correct a
edit: By trying to make the small boilerplate as your demand, I remooved all the components and it's works. So I'll find out that the solution was to correct a
'use client' component that appears in the page that contain an error 🤣Answer
Pteromalid waspOP
Amazing
@joulev It gave you a link, you can go read that link to see more
Pteromalid waspOP
Thanks!!
@Pteromalid wasp I'm trying to understand, but I think it's beyond my knowledge. 😦
edit: By trying to make the small boilerplate as your demand, I remooved all the components and it's works. So I'll find out that the solution was to correct a `'use client'` component that appears in the page that contain an error 🤣
hmm so i assume you already fixed it?
Pteromalid waspOP
Yes, it was the client error! so strange
so you fixed it, congrats, glad to hear that
yeah, trying to make a small boilerplate is often a pretty effective way to debug
it's like rubber duck debugging except you are trying to explain the code to me via simpler code and not to a rubber duck via human language
basically metadata API cannot work in client components, they only work in server components
that's likely the cause of the error
@joulev basically metadata API cannot work in client components, they only work in server components
Pteromalid waspOP
Yeah! But my code where good, since I follow all the next.js last documentation. BUT, since a component from the page was a client, and contained an error:
--> That return an error before the building of the layout, I assume
error ReferenceError: window is not defined (I wanted to get the height to customize the size of an element)--> That return an error before the building of the layout, I assume
@joulev basically metadata API cannot work in client components, they only work in server components
Pteromalid waspOP
Thanks 😉
Have a good day,
Benito
Have a good day,
Benito
@Pteromalid wasp Yeah! But my code where good, since I follow all the next.js last documentation. BUT, since a component from the page was a client, and contained an error:
`error ReferenceError: window is not defined` (I wanted to get the height to customize the size of an element)
--> That return an error before the building of the layout, I assume
Ah yeah, window is only defined in the browser, but your client component is prerendered on the server where window is not defined. You can only access window in useEffect or event handlers or similar places
@joulev Ah yeah, window is only defined in the browser, but your client component is prerendered on the server where window is not defined. You can only access window in useEffect or event handlers or similar places
Pteromalid waspOP
Yes, I change my states type from
No more errors 🙂
<number> to <number | null> and conditionnaly render the component like so:if (height === undefined) return null;No more errors 🙂
Anyway, I will leave this post since it is resolved now. Glad you solved it, have fun coding and have a good day!