Skip to content

Fix SSR support: require opt in & support both simple and advanced SSR cases#40

Merged
ai merged 5 commits intonanostores:mainfrom
jmurty:fix-regression-for-ssr
Mar 16, 2026
Merged

Fix SSR support: require opt in & support both simple and advanced SSR cases#40
ai merged 5 commits intonanostores:mainfrom
jmurty:fix-regression-for-ssr

Conversation

@jmurty
Copy link
Contributor

@jmurty jmurty commented Mar 15, 2026

Disable SSR behaviour entirely unless the new ssr option is provided, to fix regression where server-side store changes are ignored and the initial value is always rendered even in projects that don't need SSR support

Enable SSR support for simple cases, where the store's initial value is the same on server and client, by setting ssr:true

Enable SSR support for advanced cases, where server store state is updated on the server then passed to the client, by providing a function that returns the server state: ssr: () => serverState. This function is provided to React's useSyncExternalStore as the getServerSnapshot option.

…R cases

Disable SSR behaviour entirely unless the new `ssr` option is
provided, to fix regression where server-side store changes are
ignored on the initial value is always rendered even in projects that
don't need SSR support

Enable SSR support for simple cases, where the store's initial value
is the same on server and client, by setting `ssr:true`

Enable SSR support for advanced cases, where server store state is
updated on the server then passed to the client, by providing a
function that returns the server state: `ssr: () => serverState`.
This function is provided to `useSyncExternalStore` as the
`getServerSnapshot` option.
@jmurty
Copy link
Contributor Author

jmurty commented Mar 15, 2026

Hi @ai here is yet another PR, exploring a better approach for supporting SSR and client hydration per nanostores/preact#10 and nanostores/preact#12

This PR should become the priority for SSR work because it fixes a likely regression in the nanostores/react useStore behaviour on servers in the recent 1.0.1 release. See the new test "support for SSR does not break server behaviour in non-SSR projects" for this case.

@jmurty jmurty force-pushed the fix-regression-for-ssr branch from 14a106f to d6e48f9 Compare March 15, 2026 11:41

<h3>SSR</h3>

Use the `ssr` option to avoid hydration errors loading server-side rendered (SSR) pages when the browser's client store gets out of sync with the server's HTML. For example, when using Astro with `<ClientRouter />` for client-side routing and a global nanostore.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In docs we need to explain the poroblem more. Right now it looks like for SSR use ssr: true.

What is side effect of using ssr: true in wrong context? What is side-effect? In what SSR context user should not use ssr: true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ai thanks for the merge. In a few minutes I'll have (hopefully) improved documentation as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is my fix 23f6161

Do you plan to add something more?

@ai ai merged commit d7788b3 into nanostores:main Mar 16, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants