Home / Function/ createContainerWithAutomaticRestart() — astro Function Reference

createContainerWithAutomaticRestart() — astro Function Reference

Architecture documentation for the createContainerWithAutomaticRestart() function in restart.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  b10fb170_5a7e_6ebe_40a5_6958fe60be52["createContainerWithAutomaticRestart()"]
  2daba797_7568_42ad_0193_df1cabafb75a["restart.ts"]
  b10fb170_5a7e_6ebe_40a5_6958fe60be52 -->|defined in| 2daba797_7568_42ad_0193_df1cabafb75a
  50fcf1f5_a122_121d_7d4f_e4580fc86537["restartContainer()"]
  b10fb170_5a7e_6ebe_40a5_6958fe60be52 -->|calls| 50fcf1f5_a122_121d_7d4f_e4580fc86537
  80f04150_5630_d8fd_3766_b57f20410084["shouldRestartContainer()"]
  b10fb170_5a7e_6ebe_40a5_6958fe60be52 -->|calls| 80f04150_5630_d8fd_3766_b57f20410084
  style b10fb170_5a7e_6ebe_40a5_6958fe60be52 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/dev/restart.ts lines 127–236

export async function createContainerWithAutomaticRestart({
	inlineConfig,
	fs,
}: CreateContainerWithAutomaticRestart): Promise<Restart> {
	const logger = createNodeLogger(inlineConfig ?? {});
	const { userConfig, astroConfig } = await resolveConfig(inlineConfig ?? {}, 'dev', fs);
	if (astroConfig.security.csp) {
		logger.warn(
			'config',
			"Astro's Content Security Policy (CSP) does not work in development mode. To verify your CSP implementation, build the project and run the preview server.",
		);
	}
	telemetry.record(eventCliSession('dev', userConfig));

	const settings = await createSettings(
		astroConfig,
		inlineConfig?.logLevel,
		fileURLToPath(astroConfig.root),
	);

	const initialContainer = await createContainer({
		settings,
		logger: logger,
		inlineConfig,
		fs,
	});

	let resolveRestart: (value: Error | null) => void;
	let restartComplete = new Promise<Error | null>((resolve) => {
		resolveRestart = resolve;
	});

	let restart: Restart = {
		container: initialContainer,
		restarted() {
			return restartComplete;
		},
	};

	async function handleServerRestart(logMsg = '', server?: vite.ViteDevServer) {
		logger.info(null, (logMsg + ' Restarting...').trim());
		const container = restart.container;
		const result = await restartContainer(container);
		if (result instanceof Error) {
			// Failed to restart, use existing container
			resolveRestart(result);
		} else {
			// Restart success. Add new watches because this is a new container with a new Vite server
			restart.container = result;
			setupContainer();
			await attachContentServerListeners(restart.container);

			if (server) {
				// Vite expects the resolved URLs to be available
				server.resolvedUrls = result.viteServer.resolvedUrls;
			}

			resolveRestart(null);
		}
		restartComplete = new Promise<Error | null>((resolve) => {
			resolveRestart = resolve;
		});
	}

	function handleChangeRestart(logMsg: string) {
		return async function (changedFile: string) {
			if (shouldRestartContainer(restart.container, changedFile)) {
				handleServerRestart(logMsg);
			}
		};
	}

	// Set up watchers, vite restart API, and shortcuts
	function setupContainer() {
		const watcher = restart.container.viteServer.watcher;
		watcher.on('change', handleChangeRestart('Configuration file updated.'));
		watcher.on('unlink', handleChangeRestart('Configuration file removed.'));
		watcher.on('add', handleChangeRestart('Configuration file added.'));

		// Restart the Astro dev server instead of Vite's when the API is called by plugins.
		// Ignore the `forceOptimize` parameter for now.

Domain

Subdomains

Frequently Asked Questions

What does createContainerWithAutomaticRestart() do?
createContainerWithAutomaticRestart() is a function in the astro codebase, defined in packages/astro/src/core/dev/restart.ts.
Where is createContainerWithAutomaticRestart() defined?
createContainerWithAutomaticRestart() is defined in packages/astro/src/core/dev/restart.ts at line 127.
What does createContainerWithAutomaticRestart() call?
createContainerWithAutomaticRestart() calls 2 function(s): restartContainer, shouldRestartContainer.

Analyze Your Own Codebase

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

Try Supermodel Free