feat: wire up fast getEntryBySlug() lookup

This commit is contained in:
bholmesdev 2023-04-26 15:11:00 -04:00
parent ddc9afe36b
commit aa18be7fed
2 changed files with 7 additions and 18 deletions

View file

@ -81,29 +81,18 @@ export function createGetCollection({
}
export function createGetEntryBySlug({
getCollection,
getEntryImport,
getRenderEntryImport,
}: {
getCollection: ReturnType<typeof createGetCollection>;
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),
});
},
};

View file

@ -52,6 +52,6 @@ export const getCollection = createGetCollection({
});
export const getEntryBySlug = createGetEntryBySlug({
getCollection,
getEntryImport: createGlobLookup(collectionToEntryMap),
getRenderEntryImport: createGlobLookup(collectionToRenderEntryMap),
});