Best way to run parallel prefetching for SSR #1106
Answered
by
nick-0101
callumbooth
asked this question in
General
-
Based on the docs for NextJS one of the ways it suggests to handle hydration is to use the queryCache.prefetch function like so: export async function getStaticProps() {
const queryCache = new QueryCache()
await queryCache.prefetchQuery('posts', getPosts)
return {
props: {
dehydratedState: dehydrate(queryCache),
},
}
} However if you have two requests that need prefetching like so: export async function getStaticProps() {
const queryCache = new QueryCache()
await queryCache.prefetchQuery('posts', getPosts)
await queryCache.prefetchQuery('comments', getComments)
return {
props: {
dehydratedState: dehydrate(queryCache),
},
}
} Won't this cause the two requests to be run in serial rather than parallel? For now I'm using Promise.allSettled but just wondered if this is the best/recommended way? export async function getStaticProps() {
const queryCache = new QueryCache()
await Promise.allSettled([
queryCache.prefetchQuery('posts', getPosts)
queryCache.prefetchQuery('comments', getComments)
])
return {
props: {
dehydratedState: dehydrate(queryCache),
},
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
nick-0101
Jan 21, 2024
Replies: 1 comment
-
This is the recommended answer from Tkdodo https://stackoverflow.com/a/73760895/13203366 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
callumbooth
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the recommended answer from Tkdodo https://stackoverflow.com/a/73760895/13203366