diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 5ff5c8e6c..a95d83b84 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -81,29 +81,18 @@ export function createGetCollection({ } export function createGetEntryBySlug({ - getCollection, + getEntryImport, getRenderEntryImport, }: { - getCollection: ReturnType; + getEntryImport: GetEntryImport; getRenderEntryImport: GetEntryImport; }) { return async function getEntryBySlug(collection: string, slug: string) { - // This is not an optimized lookup. Should look into an O(1) implementation - // as it's probably that people will have very large collections. - const entries = await getCollection(collection); - let candidate: (typeof entries)[number] | undefined = undefined; - for (let entry of entries) { - if (entry.slug === slug) { - candidate = entry; - break; - } - } + const entryImport = await getEntryImport(collection, slug); + if (typeof entryImport !== 'function') return undefined; - if (typeof candidate === 'undefined') { - return undefined; - } + const entry = await entryImport(); - const entry = candidate; return { id: entry.id, slug: entry.slug, @@ -114,7 +103,7 @@ export function createGetEntryBySlug({ return render({ collection: entry.collection, id: entry.id, - renderEntryImport: await getRenderEntryImport(collection, entry.slug), + renderEntryImport: await getRenderEntryImport(collection, slug), }); }, }; diff --git a/packages/astro/src/content/template/virtual-mod.mjs b/packages/astro/src/content/template/virtual-mod.mjs index 7ebbf881b..11097777c 100644 --- a/packages/astro/src/content/template/virtual-mod.mjs +++ b/packages/astro/src/content/template/virtual-mod.mjs @@ -52,6 +52,6 @@ export const getCollection = createGetCollection({ }); export const getEntryBySlug = createGetEntryBySlug({ - getCollection, + getEntryImport: createGlobLookup(collectionToEntryMap), getRenderEntryImport: createGlobLookup(collectionToRenderEntryMap), });