-
Notifications
You must be signed in to change notification settings - Fork 116
Add serverSnapshot fallback to useSyncExternalStore #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add serverSnapshot fallback to useSyncExternalStore #688
Conversation
Introduces a serverSnapshot object and passes it as the server snapshot function to useSyncExternalStore in useLiveQuery. This improves compatibility with server-side rendering by providing a default snapshot value.
|
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Is this useful? I'm not sure I understand the point? You can't disable SSR in next.js? |
|
Disabling SSR in Next.js results in slower page loads, and dynamic imports requires a lot of DX overhead that is unnecessary. This also negatively impacts static export builds, since it prevents entire components from being rendered during the build phase if you put a dynamic client only import wrapper around it. |
|
The main purpose is to be able to have SSR rendered skeletons or |
|
I'm slightly nervous of this change as it suggested DB is safe to use on SSR routes, but I fear that collection.preload() in a route loader is currently unsafe with a global collection. We need to check this though as is only a suspicion, it may just slow things down and not leak data. useLiveQuery being broken in SSR acts as a safeguard against this at the moment, until we fix SSR properly. |
|
I'm testing this right now with SSR on TanStack Start, with collections created during SSR, and no issues using my patch. My patch eliminates the error entirely and you get perfect functionality. The only thing is you need to check isReady && isLoading to determine whether to show loaders. If we could merge this it would be extremely helpful to support static SSR pages with loaders and fallbackData. Also if we could add isPending === isLoading || !isReady to more easily determine whether to show loaders. This would be a huge add if we can merge this fix, there are no downsides that I have seen in any of my testing across Next.js and TanStack Start. |
|
I appreciate your efforts but this isn't what we want. We are planning on shipping actual SSR support so stay tuned for that. See #709 |
|
Will we be able to useLiveQuery on SSR routes without needing a HydrationBoundary? I noticed in the PR that serverSnapshot is still omitted in useSyncExternalStore. Not sure if it's addressed somewhere else though. Should be able to useLiveQuery without needing a HydrationBoundary without needing to disable SSR for the component that is using that hook, same as useQuery, useSWR, etc |
|
It's very common to include the 3rd parameter for useSyncExternalStore for great DX across environments, including pre-rendered SSR routes, without requiring complex HydrationBoundary setups. Both can be supported easily with 1-2 lines of code. https://github.com/nanostores/react/blob/a6ad745cc307881916944ee4d6ba04bdda978dc6/index.js#L23 Without the empty serverSnapshot parameter, I won't be able to useLiveQuery from the official package in my projects. |
|
https://react.dev/reference/react/useSyncExternalStore#adding-support-for-server-rendering You can see here how the React team recommends the 3rd parameter is used with default data to enable SSR to generate the HTML. |
|
Those all get actual data. Feel free to use your fork until we get proper SSR support in. |
|
They by default, cannot access any data since there is no data by default during an SSR render. They may "attempt" to load initial data if it is provided, but the 3rd serverSnapshot parameter is never omitted. This is done in order to prevent the missing server snapshot error from occurring in SSR. They always default to empty data. |
Introduces a serverSnapshot object and passes it as the server snapshot function to useSyncExternalStore in useLiveQuery. This improves compatibility with server-side rendering by providing a default snapshot value.
🎯 Changes
Fixes getServerSnapshot error on Next.js "use client" components without needing dynamic import.