CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-astro

Astro is a modern site builder with web best practices, performance, and DX front-of-mind.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

assets.mddocs/

Assets and Images

Image optimization, font loading, and asset management with multiple service providers.

Core APIs

Get Image

Optimizes and transforms images programmatically.

function getImage(options: UnresolvedImageTransform, imageConfig: AstroConfig['image']): Promise<GetImageResult>;

interface UnresolvedImageTransform {
  src: ImageMetadata | string;
  width?: number; height?: number; aspectRatio?: string | number;
  format?: ImageOutputFormat; quality?: ImageQuality;
  densities?: number[]; widths?: number[];
}

interface GetImageResult {
  src: string;
  srcSet: { values: SrcSetValue[]; attribute: string; };
  attributes: Record<string, any>;
}
import { getImage } from 'astro:assets';
import myImage from '../assets/photo.jpg';

const optimized = await getImage({
  src: myImage, width: 800, format: 'webp', quality: 80
});

Get Configured Image Service

function getConfiguredImageService(): Promise<ImageService>;

Infer Remote Size

function inferRemoteSize(url: string): Promise<ImageMetadata>;

interface ImageMetadata {
  src: string; width: number; height: number; format: ImageInputFormat;
}

Components

Image Component

---
import { Image } from 'astro:assets';
import localImage from '../assets/photo.jpg';
---

<!-- Local -->
<Image src={localImage} alt="Description" />

<!-- Remote (requires width/height) -->
<Image src="https://example.com/image.jpg" width={800} height={600} alt="Description" />

<!-- With transformations -->
<Image src={localImage} width={800} format="webp" quality={80} alt="Description" />

Props:

interface LocalImageProps {
  src: ImageMetadata; alt: string;
  width?: number; height?: number; format?: ImageOutputFormat; quality?: ImageQuality;
  densities?: number[]; widths?: number[];
  loading?: 'lazy' | 'eager'; decoding?: 'async' | 'sync' | 'auto';
}

interface RemoteImageProps {
  src: string; alt: string; width: number; height: number;
  format?: ImageOutputFormat; quality?: ImageQuality;
  loading?: 'lazy' | 'eager'; inferSize?: boolean;
}

Picture Component

---
import { Picture } from 'astro:assets';
import myImage from '../assets/photo.jpg';
---

<Picture src={myImage} formats={['avif', 'webp']} alt="Description" widths={[400, 800, 1200]} />

Font Component

---
import { Font } from 'astro:assets';
---

<Font cssVariable="--font-inter" preload={true} />

Props:

interface FontProps {
  cssVariable: string | { name: string; fallback?: string };
  preload?: boolean | ((font: FontData) => boolean);
}

Note: Requires font config in astro.config.mjs under experimental.fonts.

Get Font Data

function getFontData(cssVariable: string | { name: string; fallback?: string }): FontData[];

interface FontData {
  src: Array<{ url: string; format?: string; tech?: string }>;
  weight?: string; style?: string;
}

Types

type ImageOutputFormat = 'avif' | 'png' | 'webp' | 'jpeg' | 'jpg' | 'svg';
type ImageInputFormat = 'jpeg' | 'jpg' | 'png' | 'tiff' | 'webp' | 'gif' | 'svg' | 'avif';
type ImageQuality = 'low' | 'mid' | 'high' | 'max' | number;

Image Service Configuration

Sharp (Node.js only)

import { defineConfig, sharpImageService } from 'astro/config';

export default defineConfig({
  image: {
    service: sharpImageService({ limitInputPixels: 268402689 })
  }
});

Passthrough (no optimization)

import { defineConfig, passthroughImageService } from 'astro/config';

export default defineConfig({
  image: { service: passthroughImageService() }
});

Utilities (astro/assets/utils)

Image Identification

function isESMImportedImage(src: ImageMetadata | string): src is ImageMetadata;
function isRemoteImage(src: ImageMetadata | string): src is string;
function resolveSrc(src: UnresolvedImageTransform['src']): Promise<string | ImageMetadata>;

Metadata Extraction

function imageMetadata(data: Uint8Array, src?: string): Promise<Omit<ImageMetadata, 'src' | 'fsPath'>>;
function emitImageMetadata(id: string | undefined, fileEmitter?: FileEmitter): Promise<ImageMetadataWithContents | undefined>;

Remote Validation

interface RemotePattern {
  hostname?: string; pathname?: string; protocol?: string; port?: string;
}

function isRemoteAllowed(src: string, options: { domains: string[]; remotePatterns: RemotePattern[]; }): boolean;
function matchPattern(url: URL, remotePattern: RemotePattern): boolean;
import { isRemoteAllowed } from 'astro/assets/utils';

const allowed = isRemoteAllowed('https://cdn.example.com/images/photo.jpg', {
  domains: ['cdn.example.com'],
  remotePatterns: [{ protocol: 'https', hostname: '**.example.com', pathname: '/images/**' }]
});

Transform Utilities

function hashTransform(transform: ImageTransform, imageService: string, propertiesToHash: string[]): string;
function propsToFilename(filePath: string, transform: ImageTransform, hash: string): string;

SVG Imports

---
import logo from '../assets/logo.svg';
---

<!-- As image -->
<img src={logo.src} width={logo.width} height={logo.height} alt="Logo" />

<!-- As component -->
<logo width="100" height="100" />

Common Patterns

Responsive Images

<Image src={photo} widths={[400, 800, 1200]} sizes="(max-width: 600px) 400px, (max-width: 1200px) 800px, 1200px" alt="Responsive" />

CDN Integration

// astro.config.mjs
export default defineConfig({
  build: { assetsPrefix: 'https://cdn.example.com' }
});

Troubleshooting

  • Remote images: Require explicit width/height or inferSize={true}
  • SVG: Can be used as both images and components
  • Font config: Must be in astro.config.mjs before using Font component
  • Sharp: Only works in Node.js, use passthrough for edge environments

docs

assets.md

cli-and-build.md

configuration.md

container.md

content-collections.md

content-loaders.md

dev-toolbar.md

environment.md

i18n.md

index.md

integrations.md

middleware.md

server-actions.md

ssr-and-app.md

transitions.md

tile.json