Skip to main content
Learn hydration strategies for making server-rendered HTML interactive on the client in Sonamu.

Hydration Overview

TanStack Query

dehydrate/hydrate Automatic state restoration

Full Document Hydration

Document level Complete SSR

Date Serialization

dateReviver Automatic conversion

Selective Disable

disableHydrate CSR transition

Sonamu Hydration Process

1. Server: Dehydration

Serialize Query state on the server.

2. Data Injection into HTML

Generated HTML:

3. Client: Hydration

Restore state and activate React on client.

Date Serialization Handling

Problem: Date objects become strings with JSON.stringify

Solution: dateReviver

Sonamu automatically converts ISO date strings to Date objects.

Preventing Hydration Mismatch

Problem: Error when server and client HTML differ

Error message:

Solution 1: Use useEffect

Solution 2: suppressHydrationWarning

Use when server/client intentionally differ.

disableHydrate Option

Disable Hydration and Switch to CSR

How it works:
Use cases:
  • When real-time data is important (dashboards, chat, etc.)
  • When server/client rendering results intentionally differ
  • When Hydration mismatch is hard to resolve

Full Document Hydration

Sonamu hydrates the entire document (not just div#root).
Reason:
  • TanStack Router manages up to <html>, <head> tags
  • <HeadContent />, <Scripts /> components modify head/body
  • Can dynamically change SEO meta tags

Suspense and Hydration

You can render Suspense fallback on server and load actual content on client.
Why use fallback=:
  • All data is already loaded in SSR
  • Fallback won’t be visible
  • Prevents Hydration mismatch

QueryClient Configuration

Server Configuration

Client Configuration

Note: It’s good to set the same staleTime for server and client.

Router Context

Pass QueryClient via Router Context.

Debugging

Verify Hydration

Check QueryClient State

Cautions

Cautions when using Hydration: 1. Server/client HTML must match: Prevent Hydration mismatch 2. Careful with window object: window doesn’t exist on server 3. Date objects: dateReviver handles automatically but verify 4. Same QueryClient settings: Match staleTime between server and client 5. Minimize disableHydrate: Use only in special cases

Next Steps

SSR Setup

SSR basic structure

Data Preloading

How to use registerSSR

Cache Control

TanStack Query caching

TanStack Query Docs

Query detailed guide