Skip to main content
Learn how to preload data on the server and pass it to the client using Sonamu’s registerSSR.

Data Preloading Overview

registerSSR

Per-route preload config Direct backend call

SSRQuery

Type-safe queries Model/method specification

Auto Injection

Auto inject to QueryClient Hydration handling

No HTTP

No network overhead Fast response

How to Use registerSSR

Basic Structure

Process:
  1. Server receives request for /users/123
  2. path matching: /users/:idparams = { id: "123" }
  3. Execute preload function → returns SSRQuery[]
  4. Direct backend call to UserModel.getUser("C", 123) (no HTTP!)
  5. Inject result with QueryClient.setQueryData(["User", "getUser", "C", 123], result)
  6. Send HTML + dehydratedState to client
  7. Client hydrates and immediately uses data

SSRQuery Type

Important: params follows the backend method’s parameter order (excluding Context).

Practical Examples

Single Data Loading

Preload user information on user detail page.

Multiple Data Simultaneous Loading

Preload both post and comments on post detail page.

Parameter Processing

You can process URL parameters before use.

Conditional Preloading

Load different data based on conditions.

Query Key Matching

For preloaded data to match the client’s useQuery, queryKey must match exactly.

Correct Matching

Incorrect Matching

SSRRoute Options

disableHydrate

Disable Hydration and render new on client.
Use cases:
  • When server/client rendering results may differ
  • When real-time data is important
  • Resolving Hydration mismatch

cacheControl

Set Cache-Control header for SSR response.

Internal Operation Principle

1. Server Rendering Process

2. Data Injection in entry-server

3. Client Hydration

Error Handling

Handling Preload Failures

Server log:
Individual query failure doesn’t stop the entire SSR. Only failed data is reloaded on client.

Performance Optimization

1. Use Subsets

Load only needed fields to reduce transfer size.

2. Parallel Loading

Execute multiple queries simultaneously (automatically parallelized).

3. Conditional Loading

Selectively load only necessary data.

Cautions

Cautions when using registerSSR: 1. Match queryKey exactly: Server and client queryKey must be identical 2. Careful with params order: Must follow backend method parameter order exactly 3. Exclude Context: params doesn’t include Context 4. Type conversion caution: Type conversion like parseInt(params.id) needed 5. Errors are logged: Individual query failure doesn’t stop entire SSR

Next Steps

SSR Setup

SSR basic structure

Hydration Strategies

Hydration optimization

Cache Control

Caching strategies

Subset System

Data optimization