Home / File/ client.ts — astro Source File

client.ts — astro Source File

Architecture documentation for client.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  98e6d116_2eeb_b492_fcc4_2b159967fcb1["client.ts"]
  955d3da0_5530_ff15_3977_dcd12fa1bbf1["./static-html.js"]
  98e6d116_2eeb_b492_fcc4_2b159967fcb1 --> 955d3da0_5530_ff15_3977_dcd12fa1bbf1
  88d5831c_10b7_7e11_b115_8c6e411804a3["./types.js"]
  98e6d116_2eeb_b492_fcc4_2b159967fcb1 --> 88d5831c_10b7_7e11_b115_8c6e411804a3
  0fef379e_25c0_6f95_11fd_6d0f7db42b7e["preact"]
  98e6d116_2eeb_b492_fcc4_2b159967fcb1 --> 0fef379e_25c0_6f95_11fd_6d0f7db42b7e
  style 98e6d116_2eeb_b492_fcc4_2b159967fcb1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { h, hydrate, render } from 'preact';
import StaticHtml from './static-html.js';
import type { SignalLike } from './types.js';

const sharedSignalMap = new Map<string, SignalLike>();

export default (element: HTMLElement) =>
	async (
		Component: any,
		props: Record<string, any>,
		{ default: children, ...slotted }: Record<string, any>,
		{ client }: Record<string, string>,
	) => {
		if (!element.hasAttribute('ssr')) return;
		for (const [key, value] of Object.entries(slotted)) {
			props[key] = h(StaticHtml, { value, name: key });
		}
		let signalsRaw = element.dataset.preactSignals;
		if (signalsRaw) {
			const { signal } = await import('@preact/signals');
			let signals: Record<string, string | [string, number][]> = JSON.parse(
				element.dataset.preactSignals!,
			);
			for (const [propName, signalId] of Object.entries(signals)) {
				if (Array.isArray(signalId)) {
					signalId.forEach(([id, indexOrKeyInProps]) => {
						const mapValue = props[propName][indexOrKeyInProps];
						let valueOfSignal = mapValue;

						// not an property key
						if (typeof indexOrKeyInProps !== 'string') {
							valueOfSignal = mapValue[0];
							indexOrKeyInProps = mapValue[1];
						}

						if (!sharedSignalMap.has(id)) {
							const signalValue = signal(valueOfSignal);
							sharedSignalMap.set(id, signalValue);
						}
						props[propName][indexOrKeyInProps] = sharedSignalMap.get(id);
					});
				} else {
					if (!sharedSignalMap.has(signalId)) {
						const signalValue = signal(props[propName]);
						sharedSignalMap.set(signalId, signalValue);
					}
					props[propName] = sharedSignalMap.get(signalId);
				}
			}
		}

		const child = h(
			Component,
			props,
			children != null ? h(StaticHtml, { value: children }) : children,
		);

		if (client === 'only') {
			element.innerHTML = '';
			render(child, element);
		} else {
			hydrate(child, element);
		}

		// Preact has no "unmount" option, but you can use `render(null, element)`
		element.addEventListener('astro:unmount', () => render(null, element), { once: true });
	};

Domain

Dependencies

  • ./static-html.js
  • ./types.js
  • preact

Frequently Asked Questions

What does client.ts do?
client.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain.
What does client.ts depend on?
client.ts imports 3 module(s): ./static-html.js, ./types.js, preact.
Where is client.ts in the architecture?
client.ts is located at packages/integrations/preact/src/client.ts (domain: CoreAstro, directory: packages/integrations/preact/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free