feat: wire up fast getEntryBySlug() lookup
This commit is contained in:
parent
ddc9afe36b
commit
aa18be7fed
2 changed files with 7 additions and 18 deletions
|
@ -81,29 +81,18 @@ export function createGetCollection({
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createGetEntryBySlug({
|
export function createGetEntryBySlug({
|
||||||
getCollection,
|
getEntryImport,
|
||||||
getRenderEntryImport,
|
getRenderEntryImport,
|
||||||
}: {
|
}: {
|
||||||
getCollection: ReturnType<typeof createGetCollection>;
|
getEntryImport: GetEntryImport;
|
||||||
getRenderEntryImport: GetEntryImport;
|
getRenderEntryImport: GetEntryImport;
|
||||||
}) {
|
}) {
|
||||||
return async function getEntryBySlug(collection: string, slug: string) {
|
return async function getEntryBySlug(collection: string, slug: string) {
|
||||||
// This is not an optimized lookup. Should look into an O(1) implementation
|
const entryImport = await getEntryImport(collection, slug);
|
||||||
// as it's probably that people will have very large collections.
|
if (typeof entryImport !== 'function') return undefined;
|
||||||
const entries = await getCollection(collection);
|
|
||||||
let candidate: (typeof entries)[number] | undefined = undefined;
|
|
||||||
for (let entry of entries) {
|
|
||||||
if (entry.slug === slug) {
|
|
||||||
candidate = entry;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof candidate === 'undefined') {
|
const entry = await entryImport();
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const entry = candidate;
|
|
||||||
return {
|
return {
|
||||||
id: entry.id,
|
id: entry.id,
|
||||||
slug: entry.slug,
|
slug: entry.slug,
|
||||||
|
@ -114,7 +103,7 @@ export function createGetEntryBySlug({
|
||||||
return render({
|
return render({
|
||||||
collection: entry.collection,
|
collection: entry.collection,
|
||||||
id: entry.id,
|
id: entry.id,
|
||||||
renderEntryImport: await getRenderEntryImport(collection, entry.slug),
|
renderEntryImport: await getRenderEntryImport(collection, slug),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -52,6 +52,6 @@ export const getCollection = createGetCollection({
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getEntryBySlug = createGetEntryBySlug({
|
export const getEntryBySlug = createGetEntryBySlug({
|
||||||
getCollection,
|
getEntryImport: createGlobLookup(collectionToEntryMap),
|
||||||
getRenderEntryImport: createGlobLookup(collectionToRenderEntryMap),
|
getRenderEntryImport: createGlobLookup(collectionToRenderEntryMap),
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue