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
- Server receives request for
/users/123 pathmatching:/users/:idβparams = { id: "123" }- Execute
preloadfunction β returnsSSRQuery[] - Direct backend call to
UserModel.getUser("C", 123)(no HTTP!) - Inject result with
QueryClient.setQueryData(["User", "getUser", "C", 123], result) - Send HTML + dehydratedState to client
- Client hydrates and immediately uses data
SSRQuery Type
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.- 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
Guards and Input Validation
Preloads go through the sameguards and input validation as HTTP calls.
- Guard rejection: If
guardHandlerrejects, 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
NaNfromparseInt("abc")),SSRPreloadValidationExceptionfromsonamu/ssris thrown and the whole SSR response becomes400 Bad Request. Guards run first, so a preload rejected by a guard is skipped without validating its arguments. ABadRequestExceptionthrown by the model itself only skips that preload.
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
Next Steps
SSR Setup
SSR basic structure
Hydration Strategies
Hydration optimization
Cache Control
Caching strategies
Subset System
Data optimization