Home / File/ perf.ts — astro Source File

perf.ts — astro Source File

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

File typescript DevToolbar ToolbarClient 1 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  d13b2e1d_4f72_65ed_22e9_5aa089624e2b["perf.ts"]
  c19ed93d_c905_8e8a_abab_010a7ccd1966["../rules/index.js"]
  d13b2e1d_4f72_65ed_22e9_5aa089624e2b --> c19ed93d_c905_8e8a_abab_010a7ccd1966
  style d13b2e1d_4f72_65ed_22e9_5aa089624e2b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { AuditRuleWithSelector } from './index.js';

// A regular expression to match external URLs
const EXTERNAL_URL_REGEX = /^(?:[a-z+]+:)?\/\//i;

export const perf: AuditRuleWithSelector[] = [
	{
		code: 'perf-use-image-component',
		title: 'Use the Image component',
		message: 'This image could be replaced with the Image component to improve performance.',
		selector: 'img:not([data-image-component])',
		async match(element) {
			// Skip images inside framework components (React, Vue, Svelte, etc.)
			// These are wrapped in astro-island and can't directly use Astro's Image component
			if (element.closest('astro-island')) return false;

			const src = element.getAttribute('src');
			if (!src) return false;

			// Don't match data URIs, they're typically used for specific use-cases that the image component doesn't help with
			if (src.startsWith('data:')) return false;

			// Ignore images that are smaller than 20KB, most of the time the image component won't really help with these, or they're used for specific use-cases (pixel tracking, etc.)
			// Ignore this test for remote images for now, fetching them can be very slow and possibly dangerous
			if (!EXTERNAL_URL_REGEX.test(src)) {
				const imageData = await fetch(src).then((response) => response.blob());
				if (imageData.size < 20480) return false;
			}

			return true;
		},
	},
	{
		code: 'perf-use-loading-lazy',
		title: 'Unoptimized loading attribute',
		message: (element) =>
			`This ${element.nodeName} tag is below the fold and could be lazy-loaded to improve performance.`,
		selector:
			'img:not([loading]), img[loading="eager"], iframe:not([loading]), iframe[loading="eager"]',
		match(element) {
			const htmlElement = element as HTMLImageElement | HTMLIFrameElement;

			// Ignore elements that are above the fold, they should be loaded eagerly
			let currentElement = element as HTMLElement;
			let elementYPosition = 0;
			while (currentElement) {
				elementYPosition += currentElement.offsetTop;
				currentElement = currentElement.offsetParent as HTMLElement;
			}
			if (elementYPosition < window.innerHeight) return false;

			// Ignore elements using `data:` URI, the `loading` attribute doesn't do anything for these
			if (htmlElement.src.startsWith('data:')) return false;

			return true;
		},
	},
	{
		code: 'perf-use-loading-eager',
		title: 'Unoptimized loading attribute',
// ... (89 more lines)

Domain

Subdomains

Dependencies

  • ../rules/index.js

Frequently Asked Questions

What does perf.ts do?
perf.ts is a source file in the astro codebase, written in typescript. It belongs to the DevToolbar domain, ToolbarClient subdomain.
What functions are defined in perf.ts?
perf.ts defines 1 function(s): getCleanRenderingTime.
What does perf.ts depend on?
perf.ts imports 1 module(s): ../rules/index.js.
Where is perf.ts in the architecture?
perf.ts is located at packages/astro/src/runtime/client/dev-toolbar/apps/audit/rules/perf.ts (domain: DevToolbar, subdomain: ToolbarClient, directory: packages/astro/src/runtime/client/dev-toolbar/apps/audit/rules).

Analyze Your Own Codebase

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

Try Supermodel Free