Home / File/ vite-plugin-prefetch.ts — astro Source File

vite-plugin-prefetch.ts — astro Source File

Architecture documentation for vite-plugin-prefetch.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 2 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  7d0db979_04e5_6208_3779_565d025f4ceb["vite-plugin-prefetch.ts"]
  e9b74c5a_8d34_34a7_e196_5e41b87214aa["../types/astro.js"]
  7d0db979_04e5_6208_3779_565d025f4ceb --> e9b74c5a_8d34_34a7_e196_5e41b87214aa
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  7d0db979_04e5_6208_3779_565d025f4ceb --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 7d0db979_04e5_6208_3779_565d025f4ceb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type * as vite from 'vite';
import type { AstroSettings } from '../types/astro.js';

const VIRTUAL_MODULE_ID = 'astro:prefetch';
const RESOLVED_VIRTUAL_MODULE_ID = '\0' + VIRTUAL_MODULE_ID;
const prefetchInternalModuleFsSubpath = 'astro/dist/prefetch/index.js';
const prefetchCode = `import { init } from 'astro/virtual-modules/prefetch.js';init()`;

export default function astroPrefetch({ settings }: { settings: AstroSettings }): vite.Plugin {
	const prefetchOption = settings.config.prefetch;
	const prefetch = prefetchOption
		? typeof prefetchOption === 'object'
			? prefetchOption
			: {}
		: undefined;

	// Check against existing scripts as this plugin could be called multiple times
	if (prefetch && settings.scripts.every((s) => s.content !== prefetchCode)) {
		// Inject prefetch script to all pages
		settings.scripts.push({
			stage: 'page',
			content: `import { init } from 'astro/virtual-modules/prefetch.js';init()`,
		});
	}

	// Throw a normal error instead of an AstroError as Vite captures this in the plugin lifecycle
	// and would generate a different stack trace itself through esbuild.
	const throwPrefetchNotEnabledError = () => {
		throw new Error('You need to enable the `prefetch` Astro config to import `astro:prefetch`');
	};

	return {
		name: 'astro:prefetch',
		resolveId: {
			filter: {
				id: new RegExp(`^${VIRTUAL_MODULE_ID}$`),
			},
			handler() {
				if (!prefetch) throwPrefetchNotEnabledError();
				return RESOLVED_VIRTUAL_MODULE_ID;
			},
		},
		load: {
			filter: {
				id: new RegExp(`^${RESOLVED_VIRTUAL_MODULE_ID}$`),
			},
			handler() {
				if (!prefetch) throwPrefetchNotEnabledError();
				return { code: `export { prefetch } from "astro/virtual-modules/prefetch.js";` };
			},
		},
		transform: {
			filter: {
				// NOTE: Handle replacing the specifiers even if prefetch is disabled so View Transitions
				// can import the internal module and not hit runtime issues.
				id: new RegExp(`${prefetchInternalModuleFsSubpath}`),
			},
			handler(code) {
				// We perform a simple replacement with padding so that the code offset is not changed and
				// we don't have to generate a sourcemap. This has the assumption that the replaced string
				// will always be shorter than the search string to work.
				code = code
					.replace(
						'__PREFETCH_PREFETCH_ALL__', // length: 25
						`${JSON.stringify(prefetch?.prefetchAll)}`.padEnd(25),
					)
					.replace(
						'__PREFETCH_DEFAULT_STRATEGY__', // length: 29
						`${JSON.stringify(prefetch?.defaultStrategy)}`.padEnd(29),
					)
					.replace(
						'__EXPERIMENTAL_CLIENT_PRERENDER__', // length: 33
						`${JSON.stringify(settings.config.experimental.clientPrerender)}`.padEnd(33),
					);
				return { code, map: null };
			},
		},
	};
}

Domain

Subdomains

Functions

Dependencies

  • ../types/astro.js
  • vite

Frequently Asked Questions

What does vite-plugin-prefetch.ts do?
vite-plugin-prefetch.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 vite-plugin-prefetch.ts?
vite-plugin-prefetch.ts defines 1 function(s): astroPrefetch.
What does vite-plugin-prefetch.ts depend on?
vite-plugin-prefetch.ts imports 2 module(s): ../types/astro.js, vite.
Where is vite-plugin-prefetch.ts in the architecture?
vite-plugin-prefetch.ts is located at packages/astro/src/prefetch/vite-plugin-prefetch.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