Home / Function/ moveToLocation() — astro Function Reference

moveToLocation() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  e109865a_50f0_f5e3_9564_1b171d3ba481["moveToLocation()"]
  2b5c33e2_176e_e839_f05f_7f10493f4f74["router.ts"]
  e109865a_50f0_f5e3_9564_1b171d3ba481 -->|defined in| 2b5c33e2_176e_e839_f05f_7f10493f4f74
  62bdb204_6184_ebff_415b_317982b36093["updateDOM()"]
  62bdb204_6184_ebff_415b_317982b36093 -->|calls| e109865a_50f0_f5e3_9564_1b171d3ba481
  6b23b6bb_d4a4_83cb_3896_2e31c0316793["transition()"]
  6b23b6bb_d4a4_83cb_3896_2e31c0316793 -->|calls| e109865a_50f0_f5e3_9564_1b171d3ba481
  acb125f8_cfa5_7e26_14a7_da6f5dc6f5f8["samePage()"]
  e109865a_50f0_f5e3_9564_1b171d3ba481 -->|calls| acb125f8_cfa5_7e26_14a7_da6f5dc6f5f8
  style e109865a_50f0_f5e3_9564_1b171d3ba481 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/transitions/router.ts lines 174–242

const moveToLocation = (
	to: URL,
	from: URL,
	options: Options,
	pageTitleForBrowserHistory: string,
	historyState?: State,
) => {
	const intraPage = samePage(from, to);

	const targetPageTitle = document.title;
	document.title = pageTitleForBrowserHistory;

	let scrolledToTop = false;
	if (to.href !== location.href && !historyState) {
		if (options.history === 'replace') {
			const current = history.state;
			history.replaceState(
				{
					...options.state,
					index: current.index,
					scrollX: current.scrollX,
					scrollY: current.scrollY,
				},
				'',
				to.href,
			);
		} else {
			history.pushState(
				{ ...options.state, index: ++currentHistoryIndex, scrollX: 0, scrollY: 0 },
				'',
				to.href,
			);
		}
	}
	document.title = targetPageTitle;
	// now we are on the new page for non-history navigation!
	// (with history navigation page change happens before popstate is fired)
	originalLocation = to;

	// freshly loaded pages start from the top
	if (!intraPage) {
		scrollTo({ left: 0, top: 0, behavior: 'instant' });
		scrolledToTop = true;
	}

	if (historyState) {
		scrollTo(historyState.scrollX, historyState.scrollY);
	} else {
		if (to.hash) {
			// because we are already on the target page ...
			// ... what comes next is a intra-page navigation
			// that won't reload the page but instead scroll to the fragment
			history.scrollRestoration = 'auto';
			const savedState = history.state;
			location.href = to.href; // this kills the history state on Firefox
			if (!history.state) {
				history.replaceState(savedState, ''); // this restores the history state
				if (intraPage) {
					window.dispatchEvent(new PopStateEvent('popstate'));
				}
			}
		} else {
			if (!scrolledToTop) {
				scrollTo({ left: 0, top: 0, behavior: 'instant' });
			}
		}
		history.scrollRestoration = 'manual';
	}
};

Domain

Subdomains

Calls

Frequently Asked Questions

What does moveToLocation() do?
moveToLocation() is a function in the astro codebase, defined in packages/astro/src/transitions/router.ts.
Where is moveToLocation() defined?
moveToLocation() is defined in packages/astro/src/transitions/router.ts at line 174.
What does moveToLocation() call?
moveToLocation() calls 1 function(s): samePage.
What calls moveToLocation()?
moveToLocation() is called by 2 function(s): transition, updateDOM.

Analyze Your Own Codebase

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

Try Supermodel Free