Enable SSR support in useStore without breaking non-SSR usage#12
Enable SSR support in useStore without breaking non-SSR usage#12ai merged 5 commits intonanostores:mainfrom
useStore without breaking non-SSR usage#12Conversation
…ate to save bytes"
…in non-SSR usage
|
TODO: Add documentation for the new |
|
I'm realising this may be more complicated than I first thought. For server-side rendering, it's possible to have a use-case with this sequence: create store with initial value, update store value (e.g. fetch from API), render HTML Similarly on the client-side: create store with initial value (maybe from server, maybe hard-coded), update store value (e.g. from local storage), hydrate HTML To do the right thing for SSR support on the client-side, the value returned from So perhaps instead of a simple |
|
I think Do I understand correctly that we are here fixing hydration issue with specific SSR case, when server always return the initial state? For instance, there are SSR implementation when server do not return HTML until all AJAX tasks are finished (so user will not see loaders). |
|
To begin: I'm sorry for the mess my SSR code contributions have become. I hadn't thought through all the implications and use-cases properly, and getting SSR and hydration right is more difficult than I realised. Please bear with me and I'm sure we can get this solved properly. Yes, this approach so far only handles the specific SSR case where Unfortunately, I think the just released nanostores/react update might have the same problem – it solves the simple case where a default value for To handle SSR properly we need to follow the approach of the React
Where I made a mistake is treating Using To handle SSR properly and completely, I think we need two things:
How exactly the up-to-date server-side store state can get passed to the client is a another complicated question. |
…act` Apply the improved API for `ssr` options, more complete tests, and clearer documentation from the React version of `useStore`, adapted to work for Preact (i.e. via a `useEffect` hook instead of `useSyncExternalStore`)
|
Released in 1.1. Thanks! |
|
Cheers, and thank you! |
The fix for SSR usage of
useStorein #10 may introduce breaking changes for all other use-cases.This PR changes the approach, requiring the user to explicitly opt in to the SSR feature so that other use-cases will not be affected. This is necessary because we must let the user decide what state should be used at server render time, based on their use case.
Preact does not have the kind of low-level support for handling server vs client rendering provided by React'suseSyncExternalStore