Home / Function/ baseService.getSrcSet() — astro Function Reference

baseService.getSrcSet() — astro Function Reference

Architecture documentation for the baseService.getSrcSet() function in service.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  44ac12c6_fabb_dea8_00ac_961e59eb3b88["baseService.getSrcSet()"]
  f0f55fc4_9c63_b35c_86d8_8eb3e4fa20ee["service.ts"]
  44ac12c6_fabb_dea8_00ac_961e59eb3b88 -->|defined in| f0f55fc4_9c63_b35c_86d8_8eb3e4fa20ee
  0f9d5a56_bd2b_84fb_9878_5bcb5fd97446["getTargetDimensions()"]
  44ac12c6_fabb_dea8_00ac_961e59eb3b88 -->|calls| 0f9d5a56_bd2b_84fb_9878_5bcb5fd97446
  style 44ac12c6_fabb_dea8_00ac_961e59eb3b88 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/assets/services/service.ts lines 277–356

	getSrcSet(options): Array<UnresolvedSrcSetValue> {
		const { targetWidth, targetHeight } = getTargetDimensions(options);
		const aspectRatio = targetWidth / targetHeight;
		const { widths, densities } = options;
		const targetFormat = options.format ?? DEFAULT_OUTPUT_FORMAT;

		let transformedWidths = (widths ?? []).sort(sortNumeric);

		// For remote images, we don't know the original image's dimensions, so we cannot know the maximum width
		// It is ultimately the user's responsibility to make sure they don't request images larger than the original
		let imageWidth = options.width;
		let maxWidth = Infinity;

		// However, if it's an imported image, we can use the original image's width as a maximum width
		if (isESMImportedImage(options.src)) {
			imageWidth = options.src.width;
			maxWidth = imageWidth;

			// We've already sorted the widths, so we'll remove any that are larger than the original image's width
			if (transformedWidths.length > 0 && transformedWidths.at(-1)! > maxWidth) {
				transformedWidths = transformedWidths.filter((width) => width <= maxWidth);
				// If we've had to remove some widths, we'll add the maximum width back in
				transformedWidths.push(maxWidth);
			}
		}

		// Dedupe the widths
		transformedWidths = Array.from(new Set(transformedWidths));

		// Since `widths` and `densities` ultimately control the width and height of the image,
		// we don't want the dimensions the user specified, we'll create those ourselves.
		const {
			width: transformWidth,
			height: transformHeight,
			...transformWithoutDimensions
		} = options;

		// Collect widths to generate from specified densities or widths
		let allWidths: Array<{
			width: number;
			descriptor: `${number}x` | `${number}w`;
		}> = [];
		if (densities) {
			// Densities can either be specified as numbers, or descriptors (ex: '1x'), we'll convert them all to numbers
			const densityValues = densities.map((density) => {
				if (typeof density === 'number') {
					return density;
				} else {
					return parseFloat(density);
				}
			});

			// Calculate the widths for each density, rounding to avoid floats.
			const densityWidths = densityValues
				.sort(sortNumeric)
				.map((density) => Math.round(targetWidth * density));

			allWidths = densityWidths.map((width, index) => ({
				width,
				descriptor: `${densityValues[index]}x`,
			}));
		} else if (transformedWidths.length > 0) {
			allWidths = transformedWidths.map((width) => ({
				width,
				descriptor: `${width}w`,
			}));
		}

		return allWidths.map(({ width, descriptor }) => {
			const height = Math.round(width / aspectRatio);
			const transform = { ...transformWithoutDimensions, width, height };
			return {
				transform,
				descriptor,
				attributes: {
					type: `image/${targetFormat}`,
				},
			};
		});
	},

Domain

Subdomains

Frequently Asked Questions

What does baseService.getSrcSet() do?
baseService.getSrcSet() is a function in the astro codebase, defined in packages/astro/src/assets/services/service.ts.
Where is baseService.getSrcSet() defined?
baseService.getSrcSet() is defined in packages/astro/src/assets/services/service.ts at line 277.
What does baseService.getSrcSet() call?
baseService.getSrcSet() calls 1 function(s): getTargetDimensions.

Analyze Your Own Codebase

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

Try Supermodel Free