Could client.prefetchQuery() return the fetched data? #1494
-
This would be convenient for SSG / SSR. For example in next.js returning not found when a resource does not exist. // pages/[dynamicParam].js
// Here is how I am currently checking for 404
export async function getStaticProps({ params = {} }) {
const { dynamicParam } = params;
const queryClient = new QueryClient();
await queryClient.prefetchQuery(['prefix', dynamicParam], () => loadThing(dynamicParam));
const data = queryClient.getQueryData(['prefix', dynamicParam]);
return {
notFound: data === null,
props: {
dehydratedState: dehydrate(queryClient),
},
};
}
// With proposed change to prefetchQuery
export async function getStaticProps({ params = {} }) {
const { dynamicParam } = params;
const queryClient = new QueryClient();
const data = await queryClient.prefetchQuery(['prefix', dynamicParam], () => loadThing(dynamicParam));
return {
notFound: data === null,
props: {
dehydratedState: dehydrate(queryClient),
},
};
} |
Beta Was this translation helpful? Give feedback.
Answered by
tannerlinsley
Dec 20, 2020
Replies: 1 comment 1 reply
-
I think you are looking for QueryClient.fetchQuery() 😉 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
qw-in
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you are looking for QueryClient.fetchQuery() 😉