diff --git a/.changeset/four-zoos-taste.md b/.changeset/four-zoos-taste.md new file mode 100644 index 000000000000..6078bcdadb6e --- /dev/null +++ b/.changeset/four-zoos-taste.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Limits parallel imports within `getCollection()` to prevent EMFILE errors when accessing files diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 8fcbc919104c..0ad74de3e537 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -1,4 +1,5 @@ import type { MarkdownHeading } from '@astrojs/markdown-remark'; +import pLimit from 'p-limit'; import { ZodIssueCode, string as zodString } from 'zod'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { prependForwardSlash } from '../core/path.js'; @@ -80,8 +81,9 @@ export function createGetCollection({ // Always return a new instance so consumers can safely mutate it entries = [...cacheEntriesByCollection.get(collection)!]; } else { + const limit = pLimit(10); entries = await Promise.all( - lazyImports.map(async (lazyImport) => { + lazyImports.map((lazyImport) => limit(async () => { const entry = await lazyImport(); return type === 'content' ? { @@ -103,7 +105,7 @@ export function createGetCollection({ collection: entry.collection, data: entry.data, }; - }) + })) ); cacheEntriesByCollection.set(collection, entries); }