Can we statically generate output of generateMetadata ?
Answered
Barbary Lion posted this in #help-forum
Barbary LionOP
I have dynamic pages (e.g.
For those pages, I also have:
But I am not sure whether the output of
Is it? If no, how to achieve this?
app/blog/[slug]/page.js
), which I render statically using:// This is inside the app/blog/[slug]/page.js
export async function generateStaticParams() {
let posts = getBlogPosts()?.posts;
return posts.map((post) => ({
slug: post.slug,
}));
}
For those pages, I also have:
// This is also inside the app/blog/[slug]/page.js
export async function generateMetadata({ params }) {
let post = getBlogPosts()?.posts?.find((post) => post.slug === params.slug);
if (!post) {
return;
}
let { title, publishedAt: publishedTime, summary: description, image } = post.metadata;
let ogImage = image ? image : `${baseUrl}/og?title=${encodeURIComponent(title)}`;
return {
title,
description,
openGraph: {
title,
description,
type: "article",
publishedTime,
url: `${baseUrl}/blog/${post.slug}`,
images: [
{
url: ogImage,
},
],
},
twitter: {
card: "summary_large_image",
title,
description,
images: [ogImage],
},
};
}
But I am not sure whether the output of
generateMetaData
is also statically generated or not.Is it? If no, how to achieve this?
Answered by joulev
whether the metadata is statically generated is the same as whether the page is statically generated. in this case the page is static, hence the metadata is also static, don't worry
11 Replies
@Barbary Lion I have dynamic pages (e.g. `app/blog/[slug]/page.js`), which I render statically using:
// This is inside the app/blog/[slug]/page.js
export async function generateStaticParams() {
let posts = getBlogPosts()?.posts;
return posts.map((post) => ({
slug: post.slug,
}));
}
For those pages, I also have:
// This is also inside the app/blog/[slug]/page.js
export async function generateMetadata({ params }) {
let post = getBlogPosts()?.posts?.find((post) => post.slug === params.slug);
if (!post) {
return;
}
let { title, publishedAt: publishedTime, summary: description, image } = post.metadata;
let ogImage = image ? image : `${baseUrl}/og?title=${encodeURIComponent(title)}`;
return {
title,
description,
openGraph: {
title,
description,
type: "article",
publishedTime,
url: `${baseUrl}/blog/${post.slug}`,
images: [
{
url: ogImage,
},
],
},
twitter: {
card: "summary_large_image",
title,
description,
images: [ogImage],
},
};
}
But I am not sure whether the output of `generateMetaData` is also statically generated or not.
Is it? If no, how to achieve this?
whether the metadata is statically generated is the same as whether the page is statically generated. in this case the page is static, hence the metadata is also static, don't worry
Answer
Barbary LionOP
@joulev Thank you, do you have some links to back it up? I could not find that info in docs.
@Barbary Lion <@484037068239142956> Thank you, do you have some links to back it up? I could not find that info in docs.
the metadata is part of the page hence is also cached in the [full route cache](https://nextjs.org/docs/app/deep-dive/caching#full-route-cache).
but you can test it very easily. add a console.log inside the generateMetadata and see if it is logged every request.
but you can test it very easily. add a console.log inside the generateMetadata and see if it is logged every request.
Barbary LionOP
Yes, in development it logged once, then when I hit back and again entered the page, it did not log it another time.
test it in production. in development, some of the caching is disabled to prevent data becoming stale
Barbary LionOP
hmm, but the console log will it appear in browser?
no, server-side log
Barbary LionOP
Then how can I see that?
build the app in your laptop and run it
Barbary LionOP
I don't see the logs yeah
If that's true nice, thanks, but nextjs docs are so frustrating, such basic info should be there