Home / Class/ ContentLayer Class — astro Architecture

ContentLayer Class — astro Architecture

Architecture documentation for the ContentLayer class in content-layer.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  4a6efe25_e6e4_25af_3955_aede0d8f5983["ContentLayer"]
  3460caba_c22e_57de_8fed_316b77465ef7["content-layer.ts"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|defined in| 3460caba_c22e_57de_8fed_316b77465ef7
  fe0e6854_cb0f_81e5_ad60_6bca483e0f15["constructor()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| fe0e6854_cb0f_81e5_ad60_6bca483e0f15
  a0648fd8_d64d_3263_d7bb_baf8d8199b86["loading()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| a0648fd8_d64d_3263_d7bb_baf8d8199b86
  963f8134_57bf_4ccb_ae76_1a8cd242c521["watchContentConfig()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| 963f8134_57bf_4ccb_ae76_1a8cd242c521
  98b548fd_81be_aa8e_574f_c0b2a5f6fe76["unwatchContentConfig()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| 98b548fd_81be_aa8e_574f_c0b2a5f6fe76
  628b9ea0_b534_722a_c532_464eb65c9de7["dispose()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| 628b9ea0_b534_722a_c532_464eb65c9de7
  35df5732_0ce0_9b72_552a_6b58f4f43cc3["xxhash()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| 35df5732_0ce0_9b72_552a_6b58f4f43cc3
  4c30ad96_2f14_71f2_0135_fc4828f9cfb4["collectionName()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| 4c30ad96_2f14_71f2_0135_fc4828f9cfb4
  8a3d5ffd_859d_59fa_f1d4_cd4724647bb7["content()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| 8a3d5ffd_859d_59fa_f1d4_cd4724647bb7
  fa13ca14_45c4_dd38_e5f1_14d76ddb3944["sync()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| fa13ca14_45c4_dd38_e5f1_14d76ddb3944
  650841f3_f9b7_57f0_481a_fdc3aaab95b1["options()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| 650841f3_f9b7_57f0_481a_fdc3aaab95b1
  b5de73ce_33fc_df1a_559d_4a8d2c6dde27["regenerateCollectionFileManifest()"]
  4a6efe25_e6e4_25af_3955_aede0d8f5983 -->|method| b5de73ce_33fc_df1a_559d_4a8d2c6dde27

Relationship Graph

Source Code

packages/astro/src/content/content-layer.ts lines 48–381

class ContentLayer {
	#logger: Logger;
	#store: MutableDataStore;
	#settings: AstroSettings;
	#watcher?: WrappedWatcher;
	#lastConfigDigest?: string;
	#unsubscribe?: () => void;
	#markdownProcessor?: MarkdownProcessor;
	#generateDigest?: (data: Record<string, unknown> | string) => string;

	#queue: PQueue;

	constructor({ settings, logger, store, watcher }: ContentLayerOptions) {
		// The default max listeners is 10, which can be exceeded when using a lot of loaders
		watcher?.setMaxListeners(50);

		this.#logger = logger;
		this.#store = store;
		this.#settings = settings;
		if (watcher) {
			this.#watcher = createWatcherWrapper(watcher);
		}
		this.#queue = new PQueue({ concurrency: 1 });
	}

	/**
	 * Whether the content layer is currently loading content
	 */
	get loading() {
		return this.#queue.size > 0 || this.#queue.pending > 0;
	}

	/**
	 * Watch for changes to the content config and trigger a sync when it changes.
	 */
	watchContentConfig() {
		this.#unsubscribe?.();
		this.#unsubscribe = globalContentConfigObserver.subscribe(async (ctx) => {
			if (ctx.status === 'loaded' && ctx.config.digest !== this.#lastConfigDigest) {
				this.sync();
			}
		});
	}

	unwatchContentConfig() {
		this.#unsubscribe?.();
	}

	dispose() {
		this.#queue.clear();
		this.#unsubscribe?.();
		this.#watcher?.removeAllTrackedListeners();
	}

	async #getGenerateDigest() {
		if (this.#generateDigest) {
			return this.#generateDigest;
		}
		// xxhash is a very fast non-cryptographic hash function that is used to generate a content digest
		// It uses wasm, so we need to load it asynchronously.
		const { h64ToString } = await xxhash();

		this.#generateDigest = (data: unknown) => {
			const dataString = typeof data === 'string' ? data : JSON.stringify(data);
			return h64ToString(dataString);
		};

		return this.#generateDigest;
	}

	async #getLoaderContext({
		collectionName,
		loaderName = 'content',
		parseData,
		refreshContextData,
	}: {
		collectionName: string;
		loaderName: string;
		parseData: LoaderContext['parseData'];
		refreshContextData?: Record<string, unknown>;
	}): Promise<LoaderContext> {

Frequently Asked Questions

What is the ContentLayer class?
ContentLayer is a class in the astro codebase, defined in packages/astro/src/content/content-layer.ts.
Where is ContentLayer defined?
ContentLayer is defined in packages/astro/src/content/content-layer.ts at line 48.

Analyze Your Own Codebase

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

Try Supermodel Free