Next.js Discord

Discord Forum

React Query Suspense

Unanswered
Pip gall wasp posted this in #help-forum
Open in Discord
Avatar
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

Avatar
yea, it's possible
Avatar
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 me
This 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>; };
Avatar
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)
Avatar
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 🙂
Avatar
you can get the query parameters on the serverside itself as well 🙂
Avatar
Pip gall waspOP
Appreciate the help though 🙏
Avatar
yea and it's supported:
Image
Avatar
@Pip gall wasp solved?