Home / File/ index.ts — astro Source File

index.ts — astro Source File

Architecture documentation for index.ts, a typescript file in the astro codebase. 1 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 1 imports 12 functions

Entity Profile

Dependency Diagram

graph LR
  315e53ea_6d34_137c_ce5c_8c89aca90db9["index.ts"]
  e71ad64b_c09a_453c_08ca_769c94c725b5["client"]
  315e53ea_6d34_137c_ce5c_8c89aca90db9 --> e71ad64b_c09a_453c_08ca_769c94c725b5
  style 315e53ea_6d34_137c_ce5c_8c89aca90db9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/*
  NOTE: Be careful about adding dependencies or imports in this file so that it can load quickly in dev.
*/

import { internalFetchHeaders } from 'virtual:astro:adapter-config/client';

const debug = import.meta.env.DEV ? console.debug : undefined;
const inBrowser = import.meta.env.SSR === false;
// Track prefetched URLs so we don't prefetch twice
const prefetchedUrls = new Set<string>();
// Track listened anchors so we don't attach duplicated listeners
const listenedAnchors = new WeakSet<HTMLAnchorElement>();

// User-defined config for prefetch. The values are injected by vite-plugin-prefetch
// and can be undefined if not configured. But it will be set a fallback value in `init()`.
// @ts-expect-error injected global
let prefetchAll: boolean = __PREFETCH_PREFETCH_ALL__;
// @ts-expect-error injected global
let defaultStrategy: string = __PREFETCH_DEFAULT_STRATEGY__;
// @ts-expect-error injected global
let clientPrerender: boolean = __EXPERIMENTAL_CLIENT_PRERENDER__;

interface InitOptions {
	defaultStrategy?: string;
	prefetchAll?: boolean;
}

let inited = false;
/**
 * Initialize the prefetch script, only works once.
 *
 * @param defaultOpts Default options for prefetching if not already set by the user config.
 */
export function init(defaultOpts?: InitOptions) {
	if (!inBrowser) return;

	// Init only once
	if (inited) return;
	inited = true;

	debug?.(`[astro] Initializing prefetch script`);

	// Fallback default values if not set by user config
	prefetchAll ??= defaultOpts?.prefetchAll ?? false;
	defaultStrategy ??= defaultOpts?.defaultStrategy ?? 'hover';

	// In the future, perhaps we can enable treeshaking specific unused strategies
	initTapStrategy();
	initHoverStrategy();
	initViewportStrategy();
	initLoadStrategy();
}

/**
 * Prefetch links with higher priority when the user taps on them
 */
function initTapStrategy() {
	for (const event of ['touchstart', 'mousedown']) {
		document.addEventListener(
			event,
// ... (296 more lines)

Domain

Subdomains

Dependencies

  • client

Frequently Asked Questions

What does index.ts do?
index.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in index.ts?
index.ts defines 12 function(s): appendSpeculationRules, canPrefetchUrl, createViewportIntersectionObserver, elMatchesStrategy, init, initHoverStrategy, initLoadStrategy, initTapStrategy, initViewportStrategy, isSlowConnection, and 2 more.
What does index.ts depend on?
index.ts imports 1 module(s): client.
Where is index.ts in the architecture?
index.ts is located at packages/astro/src/prefetch/index.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/prefetch).

Analyze Your Own Codebase

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

Try Supermodel Free