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/:id β†’ params = { 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. API argument validation failures are the exception: they make the page 400, as described below.

Guards and Input Validation

Preloads go through the same guards and input validation as HTTP calls.
  • Guard rejection: If guardHandler rejects, only that preload is skipped. The protected data is not embedded in the HTML, and the guard applies again when the client calls the API.
  • Invalid parameters: If the arguments built by preload fail the API parameter validation (for example NaN from parseInt("abc")), SSRPreloadValidationException from sonamu/ssr is thrown and the whole SSR response becomes 400 Bad Request. Guards run first, so a preload rejected by a guard is skipped without validating its arguments. A BadRequestException thrown by the model itself only skips that preload.
The preload function only receives URL parameters, so it cannot tell whether the user is signed in. Protect data that requires authentication with guards on the API. APIs without guards run for unauthenticated SSR requests too.

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, and values that fail validation make the SSR response 400 5. Errors are logged: A guard rejection or individual query failure skips only that preload and doesn’t stop the entire SSR

Next Steps

SSR Setup

SSR basic structure

Hydration Strategies

Hydration optimization

Cache Control

Caching strategies

Subset System

Data optimization