Home / Class/ AstroIsland Class — astro Architecture

AstroIsland Class — astro Architecture

Architecture documentation for the AstroIsland class in astro-island.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  02e19bde_95de_08cd_0c93_61e01849e86b["AstroIsland"]
  7d466bc7_fd51_1f77_2b2c_baf14351cdf7["astro-island.ts"]
  02e19bde_95de_08cd_0c93_61e01849e86b -->|defined in| 7d466bc7_fd51_1f77_2b2c_baf14351cdf7
  07b3e55f_7bba_e409_58e2_3073b929ad81["disconnectedCallback()"]
  02e19bde_95de_08cd_0c93_61e01849e86b -->|method| 07b3e55f_7bba_e409_58e2_3073b929ad81
  e9a8eb59_b5bf_1bd6_cfd7_96590d01e561["connectedCallback()"]
  02e19bde_95de_08cd_0c93_61e01849e86b -->|method| e9a8eb59_b5bf_1bd6_cfd7_96590d01e561
  63d8a9e0_75ca_ac42_b069_6928c8be4c0c["childrenConnectedCallback()"]
  02e19bde_95de_08cd_0c93_61e01849e86b -->|method| 63d8a9e0_75ca_ac42_b069_6928c8be4c0c
  bdd8d9f0_00a8_70f4_1362_3c68fe814fe5["start()"]
  02e19bde_95de_08cd_0c93_61e01849e86b -->|method| bdd8d9f0_00a8_70f4_1362_3c68fe814fe5
  36b65a76_8c5c_080a_d41b_bbf347f9ffa0["attributeChangedCallback()"]
  02e19bde_95de_08cd_0c93_61e01849e86b -->|method| 36b65a76_8c5c_080a_d41b_bbf347f9ffa0

Relationship Graph

Source Code

packages/astro/src/runtime/server/astro-island.ts lines 49–211

	class AstroIsland extends HTMLElement {
		public Component: any;
		public hydrator: any;
		static observedAttributes = ['props'];

		disconnectedCallback() {
			document.removeEventListener('astro:after-swap', this.unmount);
			document.addEventListener('astro:after-swap', this.unmount, { once: true });
		}

		connectedCallback() {
			if (
				!this.hasAttribute('await-children') ||
				document.readyState === 'interactive' ||
				document.readyState === 'complete'
			) {
				this.childrenConnectedCallback();
			} else {
				// connectedCallback may run *before* children are rendered (ex. HTML streaming)
				// If SSR children are expected, but not yet rendered, wait with a mutation observer
				// for a special marker inserted when rendering islands that signals the end of the island
				const onConnected = () => {
					document.removeEventListener('DOMContentLoaded', onConnected);
					mo.disconnect();
					this.childrenConnectedCallback();
				};
				const mo = new MutationObserver(() => {
					if (
						this.lastChild?.nodeType === Node.COMMENT_NODE &&
						this.lastChild.nodeValue === 'astro:end'
					) {
						this.lastChild.remove();
						onConnected();
					}
				});
				mo.observe(this, { childList: true });
				// in case the marker comment got stripped and the mutation observer waited indefinitely,
				// also wait for DOMContentLoaded as a last resort
				document.addEventListener('DOMContentLoaded', onConnected);
			}
		}

		async childrenConnectedCallback() {
			let beforeHydrationUrl = this.getAttribute('before-hydration-url');
			if (beforeHydrationUrl) {
				await import(beforeHydrationUrl);
			}
			this.start();
		}

		async start() {
			const opts = JSON.parse(this.getAttribute('opts')!) as Record<string, any>;
			const directive = this.getAttribute('client') as directiveAstroKeys;
			if (Astro[directive] === undefined) {
				window.addEventListener(`astro:${directive}`, () => this.start(), { once: true });
				return;
			}
			try {
				await Astro[directive]!(
					async () => {
						const rendererUrl = this.getAttribute('renderer-url');
						const [componentModule, { default: hydrator }] = await Promise.all([
							import(this.getAttribute('component-url')!),
							rendererUrl ? import(rendererUrl) : () => () => {},
						]);
						const componentExport = this.getAttribute('component-export') || 'default';
						if (!componentExport.includes('.')) {
							this.Component = componentModule[componentExport];
						} else {
							this.Component = componentModule;
							for (const part of componentExport.split('.')) {
								this.Component = this.Component[part];
							}
						}
						this.hydrator = hydrator;
						return this.hydrate;
					},
					opts,
					this,
				);
			} catch (e) {

Frequently Asked Questions

What is the AstroIsland class?
AstroIsland is a class in the astro codebase, defined in packages/astro/src/runtime/server/astro-island.ts.
Where is AstroIsland defined?
AstroIsland is defined in packages/astro/src/runtime/server/astro-island.ts at line 49.

Analyze Your Own Codebase

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

Try Supermodel Free