next.js: `opengraph-image` and `twitter-image` breaks in dynamic routes
Link to the code that reproduces this issue
https://github.com/vercel/next.js/issues/49630
To Reproduce
Use opengraph-image.tsx or twitter-image.tsx in any dynamic routes (i.e. /articles/[slug] and /[...slug]).
Current vs. Expected behavior
Next doesn’t currently consider these routes, but should.
Error: Catch-all must be the last part of the URL.
From a DX perspective, having these co-located with pages makes perfect sense, but the handling is broken due to ambiguous routing. It’s not clear if a request to /articles/twitter-image is to load the Twitter image for /articles or an article with the slug twitter-image. Next defaults to the latter and therefore doesn’t support image generation on dynamic paths.
The workarounds are all hacky and require us to replicate the entire app directory just to handle dynamic cases. That’s lots of boilerplate.
Possible Solution
Could these routes be switched to instead prefix the route with the handler trigger like the image transformer does (i.e. /_next/twitter-image/articles)? This way the handler can accept route params and not conflict with page routing.
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
Node: 18.18.2
npm: 9.8.1
Yarn: 1.22.19
pnpm: 7.14.0
Relevant Packages:
next: 13.5.7-canary.23
eslint-config-next: 13.5.6
react: 18.2.0
react-dom: 18.2.0
typescript: 4.9.5
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
App Router, Metadata (metadata, generateMetadata, next/head), Routing (next/router, next/navigation, next/link)
Additional context
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 28 (12 by maintainers)
I’ve contributed a feedback to the Next.js docs that they should remove it from the documentation. Myself and other people have lost a lot of time following it.
Instead I suggest generating the dynamic opengraph image using the API folder and the
next/ogImageResponseInside the app/ folder, in create an
ogfolder withroute.tsxand follow the following principles to create your OG:As you can see at the top the dynamic data is passed through URL Search Params. like api/og?title=“My Title”&desc=“My Desc” Which will return the generated OpenGraph image in Next’s Image Response from the Edge
To consume the dynamic Open Graph image in dynamic routes you call the API route in the metadata object or generateMetadata function like so:
Looking through the docs, this section has an example with a catch-all route. I ran into the same issue with catch-all routes not able to generate the image with the error you mentioned.
Either the docs should mention a workaround or it should be supported.
Am I going crazy here or could they really just not be arsed to fix this XD, surely this would work : https://pastebin.com/A8Tm6Qj1
Since og image and icon routes are using catch-all routes to handle the both single or multiple routes. For instance:
app/opengraph-image.jscan possibly generate two cases:/opengraph-imagegenerateImageMetadata():/opengraph-image/[id]We’re not able to detect if there’s
generateImageMetadatadefined in the file before mapping the routes, and decided to create the different routes to either/opengraph-imageor/opengraph-image/[id]. That’s why we’re using a catch-all routes to handle them rn.I’ll remove the unsupported case from docs in #66355
Ran exactly on the same issue tonight. Trying to figure out what I did wrong, then going to the docs only to see that it’s supposed to work.
https://github.com/vercel/next.js/issues/57403 is a similar issue.
I hope there will be a solution, as the lack of support on catch all routes kinda defeats the whole point.
It’s working as a charm on dynamic [slug] routes though. 😉
Switching from Netlify to Vercel for the hosting solution fixed things for us. In case you’re using Netlify, I suggest you make the switch.
I trust that I’m missing something internal here, but wouldn’t this not be the same as the fs path pattern just with a prefix?
If
/blog/article/[slug]takes precedent over/blog/[...path]could/_next/opengraph-image/blog/article/[slug]not equally take precedent over/_next/opengraph-image/blog/[...path].