Home / File/ server.ts — astro Source File

server.ts — astro Source File

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

Entity Profile

Dependency Diagram

graph LR
  84ed4dca_d824_33c7_3f70_656aa15e05b9["server.ts"]
  86683eb9_f165_a541_b321_a64f8e0ef2c8["./context.js"]
  84ed4dca_d824_33c7_3f70_656aa15e05b9 --> 86683eb9_f165_a541_b321_a64f8e0ef2c8
  b7676546_8d7e_53b0_0af2_ba6296f7bad1["./types.js"]
  84ed4dca_d824_33c7_3f70_656aa15e05b9 --> b7676546_8d7e_53b0_0af2_ba6296f7bad1
  73cdec60_a6a3_4bd7_184e_5d2094806478["astro"]
  84ed4dca_d824_33c7_3f70_656aa15e05b9 --> 73cdec60_a6a3_4bd7_184e_5d2094806478
  ea2f3ad4_e59f_3552_8de6_2b408d82c6ff["svelte"]
  84ed4dca_d824_33c7_3f70_656aa15e05b9 --> ea2f3ad4_e59f_3552_8de6_2b408d82c6ff
  0abd75ee_024b_86b3_0dc2_86a02ac4a16c["server"]
  84ed4dca_d824_33c7_3f70_656aa15e05b9 --> 0abd75ee_024b_86b3_0dc2_86a02ac4a16c
  style 84ed4dca_d824_33c7_3f70_656aa15e05b9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { AstroComponentMetadata, NamedSSRLoadedRendererValue } from 'astro';
import { createRawSnippet } from 'svelte';
import { render } from 'svelte/server';
import { incrementId } from './context.js';
import type { RendererContext } from './types.js';

function check(Component: any) {
	if (typeof Component !== 'function') return false;
	// Svelte 5 generated components always accept a `$$renderer` prop (previously called `$$payload`).
	// This assumes that the SSR build does not minify it (which Astro enforces by default).
	// This isn't the best check, but the only other option otherwise is to try to render the
	// component, which is taxing. We'll leave it as a last resort for the future for now.
	const componentString = Component.toString();
	return componentString.includes('$$payload') || componentString.includes('$$renderer');
}

function needsHydration(metadata?: AstroComponentMetadata) {
	// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
	return metadata?.astroStaticSlot ? !!metadata.hydrate : true;
}

async function renderToStaticMarkup(
	this: RendererContext,
	Component: any,
	props: Record<string, any>,
	slotted: Record<string, any>,
	metadata?: AstroComponentMetadata,
) {
	const tagName = needsHydration(metadata) ? 'astro-slot' : 'astro-static-slot';

	let children = undefined;
	let $$slots: Record<string, any> | undefined = undefined;
	let idPrefix;
	if (this && this.result) {
		idPrefix = incrementId(this.result);
	}
	const renderProps: Record<string, any> = {};
	for (const [key, value] of Object.entries(slotted)) {
		// Legacy slot support
		$$slots ??= {};
		if (key === 'default') {
			$$slots.default = true;
			children = createRawSnippet(() => ({
				render: () => `<${tagName}>${value}</${tagName}>`,
			}));
		} else {
			$$slots[key] = createRawSnippet(() => ({
				render: () => `<${tagName} name="${key}">${value}</${tagName}>`,
			}));
		}
		// @render support for Svelte ^5.0
		const slotName = key === 'default' ? 'children' : key;
		renderProps[slotName] = createRawSnippet(() => ({
			render: () => `<${tagName}${key !== 'default' ? ` name="${key}"` : ''}>${value}</${tagName}>`,
		}));
	}

	const result = await render(Component, {
		props: {
			...props,
			children,
			$$slots,
			...renderProps,
		},
		idPrefix,
	});
	return { html: result.body };
}

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

export default renderer;

Domain

Subdomains

Dependencies

  • ./context.js
  • ./types.js
  • astro
  • server
  • svelte

Frequently Asked Questions

What does server.ts do?
server.ts is a source file in the astro codebase, written in typescript. It belongs to the FrameworkCore domain, IntegrationHooks subdomain.
What functions are defined in server.ts?
server.ts defines 3 function(s): check, needsHydration, renderToStaticMarkup.
What does server.ts depend on?
server.ts imports 5 module(s): ./context.js, ./types.js, astro, server, svelte.
Where is server.ts in the architecture?
server.ts is located at packages/integrations/svelte/src/server.ts (domain: FrameworkCore, subdomain: IntegrationHooks, directory: packages/integrations/svelte/src).

Analyze Your Own Codebase

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

Try Supermodel Free