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
OpenGraph images are not statically generated for dynamic routes #51147
Comments
Having this issue as well. |
You forgot the // opengraph-image.tsx
...
+ export function generateStaticParams() {
+ return [{ id: "1" }, { id: "2" }];
+ } error
(I updated all the libraries.) |
@chakkun1121 yes this is broken. There is no known workaround. You can’t use export and opengraph images in dynamic routes. It won’t work. |
I'm getting the same behavior as @chakkun1121 in Next 13.5 now too. |
Opengraph Image generation doesn't seem to work with |
In
Maybe the problem is that Next.js is generating this route Kind of odd that the images are generated in the |
@loganzartman yes I believe opengraph images ALSO don't work with catch-all routes (whether Opengraph images are pretty broken right now. There are no known workarounds for these issues. |
I was able to get something working by doing the following: In my page.js file located: import Logo from '../opengraph-image.png';
export function generateMetadata() {
return {
openGraph: {
images: [Logo.src]
}
}
} I also was not able to get it working just by dropping the file in the tree, after much trial and error I had to manually add this piece. |
I should point out that you can work around this for dynamic routes too. I created a route handler called // [slug]/og.png/route.ts
export async function generateStaticParams() {
// your logic here; this is copied directly from my dynamic route `[slug]/page.tsx`
return getAllPosts().map(({data: {slug}}) => ({slug}));
}
export async function GET(req: Request, {params}: {params: {slug: string}}) {
return new ImageResponse(...);
} In // [slug]/page.tsx
export async function generateStaticParams() {
// again, duplicated in `[slug]/og.png/route.ts`
return getAllPosts().map(({data: {slug}}) => ({slug}));
}
export function generateMetadata({params}: Props): Metadata {
const post = getPostBySlug(params.slug);
return {
openGraph: {
images: `/blog/${params.slug}/og.png`,
},
};
} |
@loganzartman thank you for your solution. Did you work with no |
@loganzartman neat idea. that worked perfectly for my site: I was able to switch to It's a shame the built-in OG support is busted but this hack will do. Thanks! |
@loganzartman Thanks for the solution ❤️ |
Verify canary release
Provide environment information
Operating System: Platform: linux Arch: x64 Version: #1 ZEN SMP PREEMPT_DYNAMIC Sun, 04 Jun 2023 11:52:10 +0000 Binaries: Node: 18.16.0 npm: 9.5.1 Yarn: 1.22.10 pnpm: 8.6.1 Relevant packages: next: 13.4.5-canary.12 eslint-config-next: 13.4.5 react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.3
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true), Metadata (metadata, generateMetadata, next/head, head.js), Static HTML Export (output: "export")
Link to the code that reproduces this issue or a replay of the bug
https://codesandbox.io/p/sandbox/reverent-feather-2sycr6
To Reproduce
output: "export"
in next.config.jsapp/[id]/page.tsx
) which hasgenerateStaticParams
opengraph-image.tsx
for that pathruntime
(which defaults to'auto'
/'nodejs'
)npm run build
)Describe the Bug
The
opengraph-image
is not generated for dynamic paths when statically rendering.In the output, it's marked as
λ (Server) server-side renders at runtime
for the dynamic paths (e.g./[id]/opengraph-image
), without listing each generated path (which/[id]
would have, e.g./1
,/2
,).I can confirm it generates a
.html
for each path inout
, but noopengraph-image
is preset.(It does work if it has no parameter in the path)
Example build output:
Expected Behavior
A
opengraph-image
would be generated for each path returned bygenerateStaticParams
, same as thepage.tsx
file would.Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
Statically rendered
NEXT-1458
The text was updated successfully, but these errors were encountered: