smolter
API

serialize

Serialize a hydration snapshot for embedding in HTML.

Compatibility

ReactAvailable in this renderer's browser integration.
React SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.
PreactAvailable in this renderer's browser integration.
Preact SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.

Import from @kasperrt/smolter/react/server @kasperrt/smolter/preact/server @kasperrt/smolter/utils

Signature

function serialize(snapshot: Snapshot): string;

Parameters

snapshot is the Router snapshot after server loading.

Returns

A JSON string with script-sensitive characters escaped.

Usage

Import this API from one of the entry points listed above. Keep server and build helpers in their dedicated subpaths.

const json = serialize(router.getSnapshot());
const script = `<script id="smolter-data" type="application/json">${json}</script>`;

Behavior and edge cases

Data must be JSON serializable. Error name and message survive; stacks and causes are omitted. Less-than signs and Unicode line separators are escaped. This is not a general HTML sanitizer; embed the result as script text, not in an attribute.

View implementation ↗