Home / Function/ simpleLoader() — astro Function Reference

simpleLoader() — astro Function Reference

Architecture documentation for the simpleLoader() function in content-layer.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  db471436_d97d_0fae_da24_bdb56669d945["simpleLoader()"]
  3460caba_c22e_57de_8fed_316b77465ef7["content-layer.ts"]
  db471436_d97d_0fae_da24_bdb56669d945 -->|defined in| 3460caba_c22e_57de_8fed_316b77465ef7
  650841f3_f9b7_57f0_481a_fdc3aaab95b1["options()"]
  650841f3_f9b7_57f0_481a_fdc3aaab95b1 -->|calls| db471436_d97d_0fae_da24_bdb56669d945
  style db471436_d97d_0fae_da24_bdb56669d945 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/content-layer.ts lines 383–453

async function simpleLoader<TData extends { id: string }>(
	handler: CollectionLoader<TData>,
	context: LoaderContext,
) {
	const unsafeData = await handler();
	const parsedData = loaderReturnSchema.safeParse(unsafeData);

	if (!parsedData.success) {
		const issue = parsedData.error.issues[0] as z.core.$ZodIssueInvalidUnion;

		// Due to this being a union, zod will always throw an "Expected array, received object" error along with the other errors.
		// This error is in the second position if the data is an array, and in the first position if the data is an object.
		const parseIssue = Array.isArray(unsafeData) ? issue.errors[0] : issue.errors[1];

		const error = parseIssue[0];
		const firstPathItem = error.path[0];

		const entry = Array.isArray(unsafeData)
			? unsafeData[firstPathItem as number]
			: unsafeData[firstPathItem as string];

		throw new AstroError({
			...AstroErrorData.ContentLoaderReturnsInvalidId,
			message: AstroErrorData.ContentLoaderReturnsInvalidId.message(context.collection, entry),
		});
	}

	const data = parsedData.data;

	context.store.clear();

	if (Array.isArray(data)) {
		for (const raw of data) {
			if (!raw.id) {
				throw new AstroError({
					...AstroErrorData.ContentLoaderInvalidDataError,
					message: AstroErrorData.ContentLoaderInvalidDataError.message(
						context.collection,
						`Entry missing ID:\n${JSON.stringify({ ...raw, id: undefined }, null, 2)}`,
					),
				});
			}
			const item = await context.parseData({ id: raw.id, data: raw });
			context.store.set({ id: raw.id, data: item });
		}
		return;
	}
	if (typeof data === 'object') {
		for (const [id, raw] of Object.entries(data)) {
			if (raw.id && raw.id !== id) {
				throw new AstroError({
					...AstroErrorData.ContentLoaderInvalidDataError,
					message: AstroErrorData.ContentLoaderInvalidDataError.message(
						context.collection,
						`Object key ${JSON.stringify(id)} does not match ID ${JSON.stringify(raw.id)}`,
					),
				});
			}
			const item = await context.parseData({ id, data: raw });
			context.store.set({ id, data: item });
		}
		return;
	}
	throw new AstroError({
		...AstroErrorData.ExpectedImageOptions,
		message: AstroErrorData.ContentLoaderInvalidDataError.message(
			context.collection,
			`Invalid data type: ${typeof data}`,
		),
	});
}

Subdomains

Called By

Frequently Asked Questions

What does simpleLoader() do?
simpleLoader() is a function in the astro codebase, defined in packages/astro/src/content/content-layer.ts.
Where is simpleLoader() defined?
simpleLoader() is defined in packages/astro/src/content/content-layer.ts at line 383.
What calls simpleLoader()?
simpleLoader() is called by 1 function(s): options.

Analyze Your Own Codebase

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

Try Supermodel Free