Request memoization vs. Query optimization
Answered
Kerry Blue Terrier posted this in #help-forum
Kerry Blue TerrierOP
Hello NextJs community!
Looking at the doc and specifically this part https://nextjs.org/docs/app/building-your-application/caching#request-memoization about Request memoization, I understand that if I make several requests on the same page (ex: one for
However I tended to do something different where I would have my complete
What's the most performant way to tackle this in your opinion?
Looking at the doc and specifically this part https://nextjs.org/docs/app/building-your-application/caching#request-memoization about Request memoization, I understand that if I make several requests on the same page (ex: one for
generateMetadata, and the other for my actual data fetching to display on page), it will get memoized and not happen twice.However I tended to do something different where I would have my complete
query for data and a specific, lighter seoQuery for generateMetadata purposes. If I get it right, with this approach, I'm essentially opting out of the request memoization feature since it's not the exact same query.What's the most performant way to tackle this in your opinion?
Answered by Ray
could you use the same query for
if so, you could do this
Project and generateMetadata?if so, you could do this
import {cache} from 'react'
const getProject = cache(async (slug) => client.fetch(query, { slug }))
export async function generateMetadata({params}) {
const project = await getProject(params.slug)
}
export default async function Project({params}) {
const project = await getProject(param.slug)
}3 Replies
@Kerry Blue Terrier Hello NextJs community!
Looking at the doc and specifically this part https://nextjs.org/docs/app/building-your-application/caching#request-memoization about Request memoization, I understand that if I make several requests on the same page (ex: one for `generateMetadata`, and the other for my actual data fetching to display on page), it will get memoized and not happen twice.
However I tended to do something different where I would have my complete `query` for data and a specific, lighter `seoQuery` for `generateMetadata` purposes. If I get it right, with this approach, I'm essentially opting out of the request memoization feature since it's not the exact same query.
What's the most performant way to tackle this in your opinion?
could you use the same query for
if so, you could do this
Project and generateMetadata?if so, you could do this
import {cache} from 'react'
const getProject = cache(async (slug) => client.fetch(query, { slug }))
export async function generateMetadata({params}) {
const project = await getProject(params.slug)
}
export default async function Project({params}) {
const project = await getProject(param.slug)
}Answer
I think the query for the project should contain all the thing for metadata?
@Ray could you use the same query for `Project` and `generateMetadata`?
if so, you could do this
ts
import {cache} from 'react'
const getProject = cache(async (slug) => client.fetch(query, { slug }))
export async function generateMetadata({params}) {
const project = await getProject(params.slug)
}
export default async function Project({params}) {
const project = await getProject(param.slug)
}
Kerry Blue TerrierOP
Hey Ray, Thanks for the answer. Yes I think that's probably the way to go 🙂