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.

File typescript 3 imports

Entity Profile

Dependency Diagram

graph LR
  b9a031cc_6133_ca6f_039f_3fe23c073318["client.ts"]
  5458ee59_7908_bf35_f257_ad630e4e60d7["solid-js"]
  b9a031cc_6133_ca6f_039f_3fe23c073318 --> 5458ee59_7908_bf35_f257_ad630e4e60d7
  52d760dc_545d_8e91_9796_96d9b52a3558["store"]
  b9a031cc_6133_ca6f_039f_3fe23c073318 --> 52d760dc_545d_8e91_9796_96d9b52a3558
  41dbb6b2_2ef8_5fd9_e7a0_ad05bd506af4["web"]
  b9a031cc_6133_ca6f_039f_3fe23c073318 --> 41dbb6b2_2ef8_5fd9_e7a0_ad05bd506af4
  style b9a031cc_6133_ca6f_039f_3fe23c073318 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { Suspense } from 'solid-js';
import { createStore, reconcile } from 'solid-js/store';
import { createComponent, hydrate, render } from 'solid-js/web';

const alreadyInitializedElements = new WeakMap<Element, any>();

export default (element: HTMLElement) =>
	(Component: any, props: any, slotted: any, { client }: { client: string }) => {
		if (!element.hasAttribute('ssr')) return;
		const isHydrate = client !== 'only';
		const bootstrap = isHydrate ? hydrate : render;

		let slot: HTMLElement | null;
		let _slots: Record<string, any> = {};
		if (Object.keys(slotted).length > 0) {
			// hydratable
			if (client !== 'only') {
				const iterator = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT, (node) => {
					if (node === element) return NodeFilter.FILTER_SKIP;
					if (node.nodeName === 'ASTRO-SLOT') return NodeFilter.FILTER_ACCEPT;
					if (node.nodeName === 'ASTRO-ISLAND') return NodeFilter.FILTER_REJECT;
					return NodeFilter.FILTER_SKIP;
				});
				while ((slot = iterator.nextNode() as HTMLElement | null))
					_slots[slot.getAttribute('name') || 'default'] = slot;
			}
			for (const [key, value] of Object.entries(slotted)) {
				if (_slots[key]) continue;
				_slots[key] = document.createElement('astro-slot');
				if (key !== 'default') _slots[key].setAttribute('name', key);
				_slots[key].innerHTML = value;
			}
		}

		const { default: children, ...slots } = _slots;
		const renderId = element.dataset.solidRenderId;
		if (alreadyInitializedElements.has(element)) {
			// update the mounted component
			alreadyInitializedElements.get(element)!(
				// reconcile will make sure to apply as little updates as possible, and also remove missing values w/o breaking reactivity
				reconcile({
					...props,
					...slots,
					children,
				}),
			);
		} else {
			const [store, setStore] = createStore({
				...props,
				...slots,
				children,
			});
			// store the function to update the current mounted component
			alreadyInitializedElements.set(element, setStore);

			const dispose = bootstrap(
				() => {
					const inner = () => createComponent(Component, store);

					if (isHydrate) {
						return createComponent(Suspense, {
							get children() {
								return inner();
							},
						});
					} else {
						return inner();
					}
				},
				element,
				{
					renderId,
				},
			);
			element.addEventListener('astro:unmount', () => dispose(), { once: true });
		}
	};

Dependencies

  • solid-js
  • store
  • web

Frequently Asked Questions

What does client.ts do?
client.ts is a source file in the astro codebase, written in typescript.
What does client.ts depend on?
client.ts imports 3 module(s): solid-js, store, web.
Where is client.ts in the architecture?
client.ts is located at packages/integrations/solid/src/client.ts (directory: packages/integrations/solid/src).

Analyze Your Own Codebase

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

Try Supermodel Free