SSR Hydration API
The hydration API provides fine-grained control over SSR mismatch detection,
abort protocol, and recovery. Most apps only need initRuntime() — the rest
is available for advanced use cases.
Hydration Lifecycle
Section titled “Hydration Lifecycle”-
startHydration()— begin hydration session -
loadMangleMapFromDOM()— read the injected mangle map from the<script>tag -
verifyMangleChecksum()— validate SHA-256 againstdata-sz-checksum -
If invalid —
abortHydration()preserves SSR HTML and CSS intact -
If valid — React hydrates normally
-
endHydration()— close session, emit audit log
Core Functions
Section titled “Core Functions”startHydration()
Section titled “startHydration()”Opens a hydration session. Call before any validation or React render.
function startHydration(): voidendHydration()
Section titled “endHydration()”Closes the current hydration session.
function endHydration(): voidabortHydration()
Section titled “abortHydration()”Aborts hydration. Blocks React from taking over the DOM.
function abortHydration(): voidisHydrationAborted()
Section titled “isHydrationAborted()”Returns true if abortHydration() has been called.
function isHydrationAborted(): booleanisHydrating()
Section titled “isHydrating()”Returns true if a hydration session is active.
function isHydrating(): booleanMangle Map Verification
Section titled “Mangle Map Verification”loadMangleMapFromDOM()
Section titled “loadMangleMapFromDOM()”Reads the injected mangle map from the #__sz_mangle__ script tag in the DOM.
function loadMangleMapFromDOM(): MangleMap | null
type MangleMap = Record<string, string>; // { 'z': 'p-4', 'y': 'bg-blue-500' }verifyMangleChecksum(map, checksum)
Section titled “verifyMangleChecksum(map, checksum)”Computes SHA-256 of the mangle map and compares to the embedded checksum.
Returns true if they match.
function verifyMangleChecksum( map: MangleMap, checksum: string): booleanverifyMangleMapIntegrity(map)
Section titled “verifyMangleMapIntegrity(map)”Validates the structure of a mangle map (non-empty, valid string values).
function verifyMangleMapIntegrity(map: MangleMap): booleanvalidateHydrationClass(mangledClass, map)
Section titled “validateHydrationClass(mangledClass, map)”Checks that a single mangled class exists in the mangle map.
function validateHydrationClass( mangledClass: string, map: MangleMap): booleanError Inspection
Section titled “Error Inspection”getHydrationErrors()
Section titled “getHydrationErrors()”Returns all recorded hydration errors for the current session.
function getHydrationErrors(): HydrationError[]
interface HydrationError { type: HydrationErrorType; message: string; class?: string; // The mangled class that failed (if applicable) timestamp: number;}clearHydrationErrors()
Section titled “clearHydrationErrors()”Clears the error list. Useful for testing.
function clearHydrationErrors(): voidError Types
Section titled “Error Types”type HydrationErrorType = | 'checksum_mismatch' // SHA-256 does not match | 'missing_manifest' // No mangle map in DOM | 'invalid_manifest' // Mangle map is malformed | 'class_not_found' // Mangled class has no mapping | 'token_invalid'; // Recovery token signature is invalidRecovery Tokens
Section titled “Recovery Tokens”Recovery tokens allow trusted mangle maps to be accepted even when the main checksum check fails. Available in development only.
verifyRecoveryToken(token, map)
Section titled “verifyRecoveryToken(token, map)”Verifies a recovery token against a mangle map.
function verifyRecoveryToken( token: string, map: MangleMap): VerificationResult
interface VerificationResult { valid: boolean; mode: RecoveryMode; expiresAt?: number;}hasRecoveryToken()
Section titled “hasRecoveryToken()”Returns true if a recovery token is present in the DOM.
function hasRecoveryToken(): booleangetRecoveryMode()
Section titled “getRecoveryMode()”Returns the current recovery mode.
type RecoveryMode = 'none' | 'csr' | 'dev-only';function getRecoveryMode(): RecoveryModeenableCSRRecovery() / disableCSRRecovery() / isCSRRecoveryAllowed()
Section titled “enableCSRRecovery() / disableCSRRecovery() / isCSRRecoveryAllowed()”Manually enable or disable client-side recovery.
function enableCSRRecovery(): voidfunction disableCSRRecovery(): voidfunction isCSRRecoveryAllowed(): booleanSSR Context
Section titled “SSR Context”isSSREnvironment()
Section titled “isSSREnvironment()”Returns true when running in an SSR context (no DOM available).
function isSSREnvironment(): booleangetSSRContext()
Section titled “getSSRContext()”Returns the current SSR context object.
function getSSRContext(): SSRContext | null
interface SSRContext { buildId: string; checksum: string; mangleMap: MangleMap;}Example: Manual Hydration Guard
Section titled “Example: Manual Hydration Guard”For advanced control over the hydration process:
import { startHydration, endHydration, abortHydration, loadMangleMapFromDOM, verifyMangleChecksum, isHydrationAborted,} from '@csszyx/runtime';
// In your app entry before React.hydrate()startHydration();
const mangleMap = loadMangleMapFromDOM();const checksum = document.documentElement.dataset.szChecksum;
if (!mangleMap || !verifyMangleChecksum(mangleMap, checksum)) { abortHydration();}
if (!isHydrationAborted()) { // Safe to hydrate hydrateRoot(document.getElementById('root'), <App />);}
endHydration();