Next.js Discord

Discord Forum

Dynamic Routes and Metadata

Answered
Polish posted this in #help-forum
Open in Discord
PolishOP
For dynamic routes is there any way to avoid having to await params both for the route function and generateMetadata. Same questions for having to query the database twice to get the data for the route and generateMetadata.
Answered by B33fb0n3
You can downgrade to <Next15. Else you need to await them, yes.

The double database call can be fixed, when you wrap your DB call function into a React.cache(() => { your code })
View full answer

13 Replies

@Polish For dynamic routes is there any way to avoid having to `await params` both for the route function and `generateMetadata`. Same questions for having to query the database twice to get the data for the route and `generateMetadata`.
You can downgrade to <Next15. Else you need to await them, yes.

The double database call can be fixed, when you wrap your DB call function into a React.cache(() => { your code })
Answer
@B33fb0n3 You can downgrade to <Next15. Else you need to await them, yes. The double database call can be fixed, when you wrap your DB call function into a React.cache(() => { your code })
PolishOP
So annoying that they made this change. What purpose does it serve to change it to await params.

Also just learned about React.cache right this second. How would you go about accessing it both in the route and generateMetadata since you have to await for the params to load?
@Polish So annoying that they made this change. What purpose does it serve to change it to `await params`. Also just learned about `React.cache` right this second. How would you go about accessing it both in the route and `generateMetadata` since you have to await for the params to load?
So annoying that they made this change. What purpose does it serve to change it to await params.
There was a picture, somewhere here on the discord that shows why the integration of an "await" is needed. To conclude that: it speeds up your app when you don't need the params

Also just learned about React.cache right this second. How would you go about accessing it both in the route and generateMetadata since you have to await for the params to load?
await the params in both and call your react cache function with your fetching method
@Polish Reading over the `React.cache` docs but I don't see any mention of how long it is valid and/if it is revalidated
the react.cache is a request based cache. So it's valid per request and not longer
@B33fb0n3 the react.cache is a request based cache. So it's valid *per request* and not longer
PolishOP
Ah. Well that is still better than 2 requests being made.
@Polish Ah. Well that is still better than 2 requests being made.
Many people use it for that, me included 😉
@B33fb0n3 Many people use it for that, me included 😉
PolishOP
Makes sense. Thanks
happy to help
@B33fb0n3 happy to help
PolishOP
Actually quick question, if I call it on the server component and then call it in the client component will it consider those 2 separate requests or will the client component use the cached version?
@Polish Actually quick question, if I call it on the server component and then call it in the client component will it consider those 2 separate requests or will the client component use the cached version?
hmmm I would test this... on the first side it's cached and it should only return the result, but on the other hand it's a call to the database and the client (hopefully) does not have the credentials to it