Home / Function/ handleHotUpdate() — astro Function Reference

handleHotUpdate() — astro Function Reference

Architecture documentation for the handleHotUpdate() function in hmr.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  2d40b353_176a_9165_78ff_5d802191ed7e["handleHotUpdate()"]
  3edca240_218c_fab7_1690_9c4cda71a3a9["hmr.ts"]
  2d40b353_176a_9165_78ff_5d802191ed7e -->|defined in| 3edca240_218c_fab7_1690_9c4cda71a3a9
  2d3b2862_fd36_3b07_5a8f_8b5d48d85545["isStyleOnlyChanged()"]
  2d40b353_176a_9165_78ff_5d802191ed7e -->|calls| 2d3b2862_fd36_3b07_5a8f_8b5d48d85545
  style 2d40b353_176a_9165_78ff_5d802191ed7e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-astro/hmr.ts lines 12–50

export async function handleHotUpdate(
	ctx: HmrContext,
	{ logger, astroFileToCompileMetadata }: HandleHotUpdateOptions,
) {
	// HANDLING 1: Invalidate compile metadata if CSS dependency updated
	//
	// If any `ctx.file` is part of a CSS dependency of any Astro file, invalidate its `astroFileToCompileMetadata`
	// so the next transform of the Astro file or Astro script/style virtual module will re-generate it
	for (const [astroFile, compileData] of astroFileToCompileMetadata) {
		const isUpdatedFileCssDep = compileData.css.some((css) => css.dependencies?.includes(ctx.file));
		if (isUpdatedFileCssDep) {
			astroFileToCompileMetadata.delete(astroFile);
		}
	}

	// HANDLING 2: Only invalidate Astro style virtual module if only style tags changed
	//
	// If only the style code has changed, e.g. editing the `color`, then we can directly invalidate
	// the Astro CSS virtual modules only. The main Astro module's JS result will be the same and doesn't
	// need to be invalidated.
	const oldCode = astroFileToCompileMetadata.get(ctx.file)?.originalCode;
	if (oldCode == null) return;
	const newCode = await ctx.read();

	if (isStyleOnlyChanged(oldCode, newCode)) {
		logger.debug('watch', 'style-only change');
		// Invalidate its `astroFileToCompileMetadata` so that the next transform of Astro style virtual module
		// will re-generate it
		astroFileToCompileMetadata.delete(ctx.file);
		return ctx.modules.filter((mod) => {
			if (!mod.id) {
				return false;
			}
			const { query } = parseAstroRequest(mod.id);
			// Only return the Astro styles that have changed, except inline style modules that are treated as SSR-only
			return query.astro && query.type === 'style' && !query.inline;
		});
	}
}

Domain

Subdomains

Frequently Asked Questions

What does handleHotUpdate() do?
handleHotUpdate() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-astro/hmr.ts.
Where is handleHotUpdate() defined?
handleHotUpdate() is defined in packages/astro/src/vite-plugin-astro/hmr.ts at line 12.
What does handleHotUpdate() call?
handleHotUpdate() calls 1 function(s): isStyleOnlyChanged.

Analyze Your Own Codebase

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

Try Supermodel Free