Home / Function/ createGetLiveCollection() — astro Function Reference

createGetLiveCollection() — astro Function Reference

Architecture documentation for the createGetLiveCollection() function in runtime.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  fe4700a6_7c11_badc_8458_a816113467e1["createGetLiveCollection()"]
  73d13646_8e80_972f_3adc_f28448b64e4d["runtime.ts"]
  fe4700a6_7c11_badc_8458_a816113467e1 -->|defined in| 73d13646_8e80_972f_3adc_f28448b64e4d
  45b9cc16_8326_90a1_a556_3b8ba93f389c["parseLiveEntry()"]
  fe4700a6_7c11_badc_8458_a816113467e1 -->|calls| 45b9cc16_8326_90a1_a556_3b8ba93f389c
  style fe4700a6_7c11_badc_8458_a816113467e1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/runtime.ts lines 260–373

export function createGetLiveCollection({
	liveCollections,
}: {
	liveCollections: LiveCollectionConfigMap;
}) {
	return async function getLiveCollection(
		collection: string,
		filter?: Record<string, unknown>,
	): Promise<LiveDataCollectionResult> {
		if (!(collection in liveCollections)) {
			return {
				error: new LiveCollectionError(
					collection,
					`Collection "${collection}" is not a live collection. Use getCollection() instead of getLiveCollection() to load regular content collections.`,
				),
			};
		}

		try {
			const context = {
				filter,
				collection,
			};

			const response = await (
				liveCollections[collection].loader as LiveLoader<any, any, Record<string, unknown>>
			)?.loadCollection?.(context);

			// Check if loader returned an error
			if (response && 'error' in response) {
				return { error: response.error };
			}

			const { schema } = liveCollections[collection];

			let processedEntries = response.entries;
			if (schema) {
				const entryResults = await Promise.all(
					response.entries.map((entry) => parseLiveEntry(entry, schema, collection)),
				);

				// Check for parsing errors
				for (const result of entryResults) {
					if (result.error) {
						// Return early on the first error
						return { error: result.error };
					}
				}

				processedEntries = entryResults.map((result) => result.entry!);
			}

			let cacheHint = response.cacheHint;
			if (cacheHint) {
				const cacheHintResult = cacheHintSchema.safeParse(cacheHint);

				if (!cacheHintResult.success) {
					return {
						error: new LiveCollectionCacheHintError(collection, undefined, cacheHintResult.error),
					};
				}
				cacheHint = cacheHintResult.data;
			}

			// Aggregate cache hints from individual entries if any
			if (processedEntries.length > 0) {
				const entryTags = new Set<string>();
				let latestModified: Date | undefined;

				for (const entry of processedEntries) {
					if (entry.cacheHint) {
						if (entry.cacheHint.tags) {
							entry.cacheHint.tags.forEach((tag) => entryTags.add(tag));
						}
						if (entry.cacheHint.lastModified instanceof Date) {
							if (latestModified === undefined || entry.cacheHint.lastModified > latestModified) {
								latestModified = entry.cacheHint.lastModified;
							}
						}
					}
				}

Subdomains

Frequently Asked Questions

What does createGetLiveCollection() do?
createGetLiveCollection() is a function in the astro codebase, defined in packages/astro/src/content/runtime.ts.
Where is createGetLiveCollection() defined?
createGetLiveCollection() is defined in packages/astro/src/content/runtime.ts at line 260.
What does createGetLiveCollection() call?
createGetLiveCollection() calls 1 function(s): parseLiveEntry.

Analyze Your Own Codebase

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

Try Supermodel Free