React Query Suspense
Unanswered
Pip gall wasp posted this in #help-forum
Pip gall waspOP
I have a site that uses a static export - I am currently trying to figure out if during the build phase whether it is possible to return a suspenseful state that only gets fufilled on the client using React Query suspense. Is this possible?
10 Replies
yea, it's possible
Pip gall waspOP
Is it possible to do this and also determine that the value returned from
useSuspenseQuery
is always defined? The only way I have been able to achieve this is by returning a null value during SSG which defeats the purpose for meThis is the only way I've been able to achieve the desired effect (SSG rendered with the loading state, and then fulfilled with the API data on the client) - however the error will bubble and next will abort the build
``tsx
export const TestPage = () => {
const { data } = useSuspenseQuery({
queryKey: ['key'],
queryFn: async () => {
if (typeof window === 'undefined') {
throw new Error('SSG render');
}
return 123;
},
});
return <div>{data}</div>;
};
no, that's not possible, because it will be handled clientside. So it will be refetched over and over again when there is a client.
When you want to build your page statically (but still with dynamic data) you can use SSR (server side rendering) and fetch data on the server. This data then can be passed to where you need it (server component & client components)
When you want to build your page statically (but still with dynamic data) you can use SSR (server side rendering) and fetch data on the server. This data then can be passed to where you need it (server component & client components)
Pip gall waspOP
That's a shame - part of the query key depends on query paremeters so unfortunately this isn't possible with a static export. Thanks for your help 🙂
you can get the query parameters on the serverside itself as well 🙂
Pip gall waspOP
We're using [this](https://nextjs.org/docs/pages/building-your-application/deploying/static-exports) so unfortunately not :/
Appreciate the help though 🙏
@Pip gall wasp solved?