capri-js / capri

Build static sites with interactive islands
https://capri.build
MIT License
208 stars 5 forks source link

[react] Suspense for Data Fetching + Static Site Generation? #39

Closed fgnass closed 1 year ago

fgnass commented 2 years ago

We would like to use Suspense for Data Fetching in an SSG setup (Capri + React).

The idea is to use renderToPipeableStream() + onAllReady() or renderToReadableStream() + await stream.allReady to get the final markup after all suspense boundaries have resolved.

This already works when using SWR 1.x, but SWR 2.x will no longer run on the server as it has no way of transferring the data to the client.

I understand that the way forward for SSR will be React Server Components, but this doesn't sound like a viable solution for generating static markup.

I could imagine that using a primitive like react-fetch would be a suitable option in the future, but currently react-dom/serverdoes not implement getCacheForType() so this does not work (yet?).

I guess the question boils down to this: Will there be a supported way to use Suspense for Data Fetching in an SSG context, or will we need to fetch the data outside of React with a Next.js-style getStaticProps pattern?

fgnass commented 1 year ago

I'm closing this for now, as the Capri react template now uses the data loading infrastructure provided by react-router v6. The same can be achieved with TanStack Router.