Next.js Discord

Discord Forum

Custom Meta Tags in Next.js 14 for Social Media Previews Slow Down Page Rendering

Unanswered
American Shorthair posted this in #help-forum
Open in Discord
American ShorthairOP
Hey,

I'm working with Next.js 14 and looking to customize meta tags dynamically for enhanced website previews on social platforms like Discord and Twitter. The metadata I need to display requires backend calls, which I've implemented using generateMetadata() as per the [Next.js documentation](https://nextjs.org/docs/app/api-reference/functions/generate-metadata).

However, I've encountered a performance issue: incorporating generateMetadata() slows down my page rendering significantly. Prior to this, the initial HTML would load almost instantaneously, with API data fetching occurring post-load.

Previously, I used a workaround where I'd extract the user agent from the request object to determine if the requester was Discord or Twitter. If so, I'd call my API backend to generate the necessary metadata; if not, I'd bypass this step to avoid unnecessary load time, as it's redundant for regular browser display. With the introduction of generateMetadata(), I no longer have access to the request object or any means to identify the user agent.

I'm looking for suggestions or best practices on how to approach this. Is there a way to access the user agent or an alternative method to conditionally generate meta tags without compromising page load speed?

1 Reply

American ShorthairOP
Just to be clear, it is slow but as expected to be slow (100ms which is the timing for the server to respond), I am looking to optimize this by skipping the request when it is not necessary.