Next.js Discord

Discord Forum

React Query Suspense

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

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>; };
@Pip gall wasp 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
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)
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 🙂
Pip gall waspOP
Appreciate the help though 🙏
@Pip gall wasp We're using [this](https://nextjs.org/docs/pages/building-your-application/deploying/static-exports) so unfortunately not :/
yea and it's supported:
@Pip gall wasp solved?