Deep Dive: SSR Hydration
Archibald supports seamless session hydration from server to client. This ensures that the user's authentication state is available immediately upon page load without extra client-side requests.
How it works
The following diagram illustrates the lifecycle of a session token from the initial request to client-side hydration:
Detailed Process
- Request Extraction: When the browser requests a page, it automatically includes the authentication cookies (
nctfor session,ncrfor refresh). TheAuthModuleon the server extracts and validates these tokens. - Server Prefetch: During the server-side rendering phase, the
SessionClient(running on the server) callsprefetchUser(). This populates the internal cache with the current user's profile. - Serialization: Archibald serializes the prefetched state into the initial HTML payload via the
window.__INITIAL_DATA_CACHE__andwindow.__INITIAL_APP_CACHE__script blocks. - Client Initialization: When the React application bootstraps in the browser, the client-side
SessionClientdetects the serialized state and hydrates its internal cache immediately. - Instant Availability: Hooks like
useUseranduseIsLoggedInreturn the correct data on the very first render, preventing layout shifts or "flickers" of unauthenticated content.
Configuration
To enable this behavior, the authenticateOnServer option must be set to true in the SessionClient configuration.
new SessionClient({
authenticateOnServer: true,
// ...
});
Troubleshooting
If hydration is not working, check the following:
- Ensure that the
AuthModuleis correctly configured on the server. - Verify that the
nct(session) cookie is being sent by the browser. - Check that the
SessionClientProvideris wrapping your application.