import { stringify } from 'devalue'; import { getRequestContext } from '../runtime/requestContext'; /** One entry in the per-render dedup registry (`ctx.islandProps`): a unique serialized payload's ref id and how many islands emitted it. */ export interface IslandPropsEntry { id: string; emitCount: number; } /** * Serialize a hydratable island's props via devalue and register them in the per-render dedup registry, returning the * stable ref id the preprocessor emits as `props-ref`. After SSR, `ComponentRegistry`'s HTMLRewriter pass writes each * payload as a ``; } /** * Insert one island's props block immediately before `el`, the first `` referencing it. * `ComponentRegistry`'s HTMLRewriter pass calls this for every island in document order: `propsById` maps a ref id to * its payload and emit count, while `emitted` records ids already written so islands sharing a byte-identical payload * reuse the one block. An island with no `props-ref`, or a ref absent from the registry, is left untouched. */ export function injectIslandPropsBlock(el: HTMLRewriterTypes.Element, propsById: Map, emitted: Set): void { const ref = el.getAttribute('props-ref'); if (!ref || emitted.has(ref)) { return; } const entry = propsById.get(ref); if (!entry) { return; } emitted.add(ref); el.before(renderIslandPropsScript(ref, entry.json, entry.emitCount), { html: true }); }