Home / Function/ transition() — astro Function Reference

transition() — astro Function Reference

Architecture documentation for the transition() function in router.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  6b23b6bb_d4a4_83cb_3896_2e31c0316793["transition()"]
  2b5c33e2_176e_e839_f05f_7f10493f4f74["router.ts"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|defined in| 2b5c33e2_176e_e839_f05f_7f10493f4f74
  f381805f_958f_3bc5_bf46_7acaa731699b["navigate()"]
  f381805f_958f_3bc5_bf46_7acaa731699b -->|calls| 6b23b6bb_d4a4_83cb_3896_2e31c0316793
  aad39bf4_3804_f21c_99b5_906d119040a5["onPopState()"]
  aad39bf4_3804_f21c_99b5_906d119040a5 -->|calls| 6b23b6bb_d4a4_83cb_3896_2e31c0316793
  f381805f_958f_3bc5_bf46_7acaa731699b["navigate()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| f381805f_958f_3bc5_bf46_7acaa731699b
  a555368b_967a_86b4_3484_9737bfbddef8["abortAndRecreateMostRecentNavigation()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| a555368b_967a_86b4_3484_9737bfbddef8
  3b2e9589_83d6_19e2_100c_4c1b800e1d48["transitionEnabledOnThisPage()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| 3b2e9589_83d6_19e2_100c_4c1b800e1d48
  acb125f8_cfa5_7e26_14a7_da6f5dc6f5f8["samePage()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| acb125f8_cfa5_7e26_14a7_da6f5dc6f5f8
  e109865a_50f0_f5e3_9564_1b171d3ba481["moveToLocation()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| e109865a_50f0_f5e3_9564_1b171d3ba481
  5a0af174_e810_2fce_864c_1a4fecf4bc03["fetchHTML()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| 5a0af174_e810_2fce_864c_1a4fecf4bc03
  a3f6f1d3_8ee5_3af7_2e71_96af3c87e27c["preloadStyleLinks()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| a3f6f1d3_8ee5_3af7_2e71_96af3c87e27c
  11d91b21_1089_a190_f404_3fb15118fd0f["prepareForClientOnlyComponents()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| 11d91b21_1089_a190_f404_3fb15118fd0f
  62bdb204_6184_ebff_415b_317982b36093["updateDOM()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| 62bdb204_6184_ebff_415b_317982b36093
  a2d4df41_22b3_3eaa_a73b_c26153c5e44b["getFallback()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| a2d4df41_22b3_3eaa_a73b_c26153c5e44b
  fa3e9103_6d44_552d_0306_d692069ae5ed["runScripts()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| fa3e9103_6d44_552d_0306_d692069ae5ed
  bd9805f3_e8d7_ffa7_9b83_96a4fb55a0b5["announce()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| bd9805f3_e8d7_ffa7_9b83_96a4fb55a0b5
  style 6b23b6bb_d4a4_83cb_3896_2e31c0316793 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/transitions/router.ts lines 340–575

async function transition(
	direction: Direction,
	from: URL,
	to: URL,
	options: Options,
	historyState?: State,
) {
	// The most recent navigation always has precedence
	// Yes, there can be several navigation instances as the user can click links
	// while we fetch content or simulate view transitions. Even synchronous creations are possible
	// e.g. by calling navigate() from an transition event.
	// Invariant: all but the most recent navigation are already aborted.

	const currentNavigation = abortAndRecreateMostRecentNavigation();

	// not ours
	if (!transitionEnabledOnThisPage() || location.origin !== to.origin) {
		if (currentNavigation === mostRecentNavigation) mostRecentNavigation = undefined;
		location.href = to.href;
		return;
	}

	const navigationType = historyState
		? 'traverse'
		: options.history === 'replace'
			? 'replace'
			: 'push';

	if (navigationType !== 'traverse') {
		updateScrollPosition({ scrollX, scrollY });
	}
	if (samePage(from, to) && !options.formData) {
		if ((direction !== 'back' && to.hash) || (direction === 'back' && from.hash)) {
			moveToLocation(to, from, options, document.title, historyState);
			if (currentNavigation === mostRecentNavigation) mostRecentNavigation = undefined;
			return;
		}
	}

	const prepEvent = await doPreparation(
		from,
		to,
		direction,
		navigationType,
		options.sourceElement,
		options.info,
		currentNavigation!.controller.signal,
		options.formData,
		defaultLoader,
	);
	if (prepEvent.defaultPrevented || prepEvent.signal.aborted) {
		if (currentNavigation === mostRecentNavigation) mostRecentNavigation = undefined;
		if (!prepEvent.signal.aborted) {
			// not aborted -> delegate to browser
			location.href = to.href;
		}
		// and / or exit
		return;
	}

	async function defaultLoader(preparationEvent: TransitionBeforePreparationEvent) {
		const href = preparationEvent.to.href;
		const init: RequestInit = { signal: preparationEvent.signal };
		if (preparationEvent.formData) {
			init.method = 'POST';
			const form =
				preparationEvent.sourceElement instanceof HTMLFormElement
					? preparationEvent.sourceElement
					: preparationEvent.sourceElement instanceof HTMLElement &&
							'form' in preparationEvent.sourceElement
						? (preparationEvent.sourceElement.form as HTMLFormElement)
						: preparationEvent.sourceElement?.closest('form');
			// Form elements without enctype explicitly set default to application/x-www-form-urlencoded.
			// In order to maintain compatibility with Astro 4.x, we need to check the value of enctype
			// on the attributes property rather than accessing .enctype directly. Astro 5.x may
			// introduce defaulting to application/x-www-form-urlencoded as a breaking change, and then
			// we can access .enctype directly.
			//
			// Note: getNamedItem can return null in real life, even if TypeScript doesn't think so, hence
			// the ?.
			init.body =

Domain

Subdomains

Frequently Asked Questions

What does transition() do?
transition() is a function in the astro codebase, defined in packages/astro/src/transitions/router.ts.
Where is transition() defined?
transition() is defined in packages/astro/src/transitions/router.ts at line 340.
What does transition() call?
transition() calls 12 function(s): abortAndRecreateMostRecentNavigation, announce, fetchHTML, getFallback, moveToLocation, navigate, preloadStyleLinks, prepareForClientOnlyComponents, and 4 more.
What calls transition()?
transition() is called by 2 function(s): navigate, onPopState.

Analyze Your Own Codebase

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

Try Supermodel Free