Home / Function/ prefetch() — astro Function Reference

prefetch() — astro Function Reference

Architecture documentation for the prefetch() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  1502f987_7806_fcef_2f8e_55d799080686["prefetch()"]
  315e53ea_6d34_137c_ce5c_8c89aca90db9["index.ts"]
  1502f987_7806_fcef_2f8e_55d799080686 -->|defined in| 315e53ea_6d34_137c_ce5c_8c89aca90db9
  24714cbf_c269_c51b_a951_fe6138916c1c["initTapStrategy()"]
  24714cbf_c269_c51b_a951_fe6138916c1c -->|calls| 1502f987_7806_fcef_2f8e_55d799080686
  90b6d96b_d7f2_cf1e_695c_fcf15d5c6440["initHoverStrategy()"]
  90b6d96b_d7f2_cf1e_695c_fcf15d5c6440 -->|calls| 1502f987_7806_fcef_2f8e_55d799080686
  41ad7978_6a9c_8215_6479_75ce204d7cbd["createViewportIntersectionObserver()"]
  41ad7978_6a9c_8215_6479_75ce204d7cbd -->|calls| 1502f987_7806_fcef_2f8e_55d799080686
  f350ee51_b3bd_e845_7222_24e2d4d0756a["initLoadStrategy()"]
  f350ee51_b3bd_e845_7222_24e2d4d0756a -->|calls| 1502f987_7806_fcef_2f8e_55d799080686
  ea9541b4_bd9d_6d4f_5137_70ec3d7865b1["canPrefetchUrl()"]
  1502f987_7806_fcef_2f8e_55d799080686 -->|calls| ea9541b4_bd9d_6d4f_5137_70ec3d7865b1
  48f302df_372c_1a3e_b7a1_ac529c030249["appendSpeculationRules()"]
  1502f987_7806_fcef_2f8e_55d799080686 -->|calls| 48f302df_372c_1a3e_b7a1_ac529c030249
  style 1502f987_7806_fcef_2f8e_55d799080686 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/prefetch/index.ts lines 216–248

export function prefetch(url: string, opts?: PrefetchOptions) {
	// Remove url hash to avoid prefetching the same URL multiple times
	url = url.replace(/#.*/, '');

	const ignoreSlowConnection = opts?.ignoreSlowConnection ?? false;
	if (!canPrefetchUrl(url, ignoreSlowConnection)) return;
	prefetchedUrls.add(url);

	// Prefetch with speculationrules if `clientPrerender` is enabled and supported
	// NOTE: This condition is tree-shaken if `clientPrerender` is false as its a static value
	if (clientPrerender && HTMLScriptElement.supports?.('speculationrules')) {
		debug?.(`[astro] Prefetching ${url} with <script type="speculationrules">`);
		appendSpeculationRules(url, opts?.eagerness ?? 'immediate');
	}
	// Prefetch with link if supported
	else if (document.createElement('link').relList?.supports?.('prefetch')) {
		debug?.(`[astro] Prefetching ${url} with <link rel="prefetch">`);
		const link = document.createElement('link');
		link.rel = 'prefetch';
		link.setAttribute('href', url);
		document.head.append(link);
	}
	// Otherwise, fallback prefetch with fetch
	else {
		debug?.(`[astro] Prefetching ${url} with fetch`);
		// Apply adapter-specific headers for internal fetches
		const headers = new Headers();
		for (const [key, value] of Object.entries(internalFetchHeaders) as [string, string][]) {
			headers.set(key, value);
		}
		fetch(url, { priority: 'low', headers });
	}
}

Domain

Subdomains

Frequently Asked Questions

What does prefetch() do?
prefetch() is a function in the astro codebase, defined in packages/astro/src/prefetch/index.ts.
Where is prefetch() defined?
prefetch() is defined in packages/astro/src/prefetch/index.ts at line 216.
What does prefetch() call?
prefetch() calls 2 function(s): appendSpeculationRules, canPrefetchUrl.
What calls prefetch()?
prefetch() is called by 4 function(s): createViewportIntersectionObserver, initHoverStrategy, initLoadStrategy, initTapStrategy.

Analyze Your Own Codebase

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

Try Supermodel Free