Home / File/ server-v17.ts — astro Source File

server-v17.ts — astro Source File

Architecture documentation for server-v17.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.

File typescript CoreAstro CoreMiddleware 4 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  b97b7573_9108_45c6_0cb4_8a2c7176f277["server-v17.ts"]
  5d6f4037_5bea_5d26_d23c_0c9bcb7db079["./static-html.js"]
  b97b7573_9108_45c6_0cb4_8a2c7176f277 --> 5d6f4037_5bea_5d26_d23c_0c9bcb7db079
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  b97b7573_9108_45c6_0cb4_8a2c7176f277 --> f16d8c76_2866_6150_bd14_0347b59abfe9
  d9988dd0_c044_f9d2_85cd_a31a0a2bdf80["react"]
  b97b7573_9108_45c6_0cb4_8a2c7176f277 --> d9988dd0_c044_f9d2_85cd_a31a0a2bdf80
  72c29c28_0c5e_16fd_13f7_f42c45a83f93["server"]
  b97b7573_9108_45c6_0cb4_8a2c7176f277 --> 72c29c28_0c5e_16fd_13f7_f42c45a83f93
  style b97b7573_9108_45c6_0cb4_8a2c7176f277 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { AstroComponentMetadata, NamedSSRLoadedRendererValue } from 'astro';
import React from 'react';
import ReactDOM from 'react-dom/server';
import StaticHtml from './static-html.js';

const slotName = (str: string) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
const reactTypeof = Symbol.for('react.element');

function check(Component: any, props: Record<string, any>, children: any) {
	// Note: there are packages that do some unholy things to create "components".
	// Checking the $$typeof property catches most of these patterns.
	if (typeof Component === 'object') {
		return Component['$$typeof']?.toString().slice('Symbol('.length).startsWith('react');
	}
	if (typeof Component !== 'function') return false;
	if (Component.name === 'QwikComponent') return false;

	if (Component.prototype != null && typeof Component.prototype.render === 'function') {
		return React.Component.isPrototypeOf(Component) || React.PureComponent.isPrototypeOf(Component);
	}

	let isReactComponent = false;
	function Tester(...args: Array<any>) {
		try {
			const vnode = Component(...args);
			if (vnode && vnode['$$typeof'] === reactTypeof) {
				isReactComponent = true;
			}
		} catch {}

		return React.createElement('div');
	}

	renderToStaticMarkup(Tester, props, children, {} as any);

	return isReactComponent;
}

async function renderToStaticMarkup(
	Component: any,
	props: Record<string, any>,
	{ default: children, ...slotted }: Record<string, any>,
	metadata?: AstroComponentMetadata,
) {
	delete props['class'];
	const slots: Record<string, any> = {};
	for (const [key, value] of Object.entries(slotted)) {
		const name = slotName(key);
		slots[name] = React.createElement(StaticHtml, { value, name });
	}
	// Note: create newProps to avoid mutating `props` before they are serialized
	const newProps = {
		...props,
		...slots,
	};
	const newChildren = children ?? props.children;
	if (newChildren != null) {
		newProps.children = React.createElement(StaticHtml, {
			// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
			hydrate: metadata?.astroStaticSlot ? !!metadata.hydrate : true,
			value: newChildren,
		});
	}
	const vnode = React.createElement(Component, newProps);
	let html: string;
	if (metadata?.hydrate) {
		html = ReactDOM.renderToString(vnode);
	} else {
		html = ReactDOM.renderToStaticMarkup(vnode);
	}
	return { html };
}

const renderer: NamedSSRLoadedRendererValue = {
	name: '@astrojs/react',
	check,
	renderToStaticMarkup,
	supportsAstroStaticSlot: true,
};

export default renderer;

Domain

Subdomains

Dependencies

  • ./static-html.js
  • astro
  • react
  • server

Frequently Asked Questions

What does server-v17.ts do?
server-v17.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in server-v17.ts?
server-v17.ts defines 3 function(s): check, renderToStaticMarkup, slotName.
What does server-v17.ts depend on?
server-v17.ts imports 4 module(s): ./static-html.js, astro, react, server.
Where is server-v17.ts in the architecture?
server-v17.ts is located at packages/integrations/react/src/server-v17.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/react/src).

Analyze Your Own Codebase

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

Try Supermodel Free