An open API service indexing awesome lists of open source software.

https://github.com/trpc/use-client-stream-hydration

Streamed hydration with "use client"-components
https://github.com/trpc/use-client-stream-hydration

Last synced: about 1 year ago
JSON representation

Streamed hydration with "use client"-components

Awesome Lists containing this project

README

          

Streamed hydration with `"use client"` components using `@tanstack/react-query`.

Demo: https://use-client-stream-hydration.vercel.app/

## How this works

- We use `suspense: true` in our `useQuery()`-calls
- The API-requests from the `useQuery`-hooks get initialize on the server
- We use Next.js' `useServerInsertedHTML()` to insert ``-tags with dehydrated state of the request results.
- The browser subscribes to the data changes in said `<script>`-tags and updates the React Query cache

### Known limitations

- If the browser tree re-renders while the data is fetching, we'll initialize the request in the browser
- This is not "true" SSR and requires JS - the server does stream data to the browser, but if you View Source, you'll see that the DOM will only contain the suspense boundaries' fallbacks and not the DOM

## Prior art

- Apollo RFC: _"The Next.js "App Router", React Server Component & "SSR with Suspense"_ - https://github.com/apollographql/apollo-client-nextjs/blob/pr/RFC-2/RFC.md
- urql PR: _"support Next 13 and React Server Components"_ - https://github.com/urql-graphql/urql/pull/3214