0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

refactor: add cache layer

This commit is contained in:
bholmesdev 2023-04-27 12:34:30 -04:00
parent 7f77913b9f
commit 1c3bfdc6b3
2 changed files with 13 additions and 4 deletions

View file

@ -202,6 +202,7 @@ export async function createContentTypesGenerator({
fileUrl: event.entry, fileUrl: event.entry,
contentEntryType, contentEntryType,
fs, fs,
invalidateCache: true,
}); });
if (!(collectionKey in contentTypes)) { if (!(collectionKey in contentTypes)) {
addCollection(contentTypes, collectionKey); addCollection(contentTypes, collectionKey);
@ -225,6 +226,7 @@ export async function createContentTypesGenerator({
fileUrl: event.entry, fileUrl: event.entry,
contentEntryType, contentEntryType,
fs, fs,
invalidateCache: true,
}); });
if (contentTypes[collectionKey]?.[entryKey]?.slug !== changedSlug) { if (contentTypes[collectionKey]?.[entryKey]?.slug !== changedSlug) {
setEntry(contentTypes, collectionKey, entryKey, changedSlug); setEntry(contentTypes, collectionKey, entryKey, changedSlug);

View file

@ -442,6 +442,7 @@ export async function getStringifiedLookupMap({
return JSON.stringify(filePathByLookupId); return JSON.stringify(filePathByLookupId);
} }
const frontmatterSlugCache = new Map<string, string>();
/** /**
* Check for slug in content entry frontmatter and validate the type, * Check for slug in content entry frontmatter and validate the type,
* falling back to the `generatedSlug` if none is found. * falling back to the `generatedSlug` if none is found.
@ -453,6 +454,7 @@ export async function getEntrySlug({
contentEntryType, contentEntryType,
fileUrl, fileUrl,
fs, fs,
invalidateCache = false,
}: { }: {
fs: typeof fsMod; fs: typeof fsMod;
id: string; id: string;
@ -460,11 +462,16 @@ export async function getEntrySlug({
generatedSlug: string; generatedSlug: string;
fileUrl: URL; fileUrl: URL;
contentEntryType: Pick<ContentEntryType, 'getEntryInfo'>; contentEntryType: Pick<ContentEntryType, 'getEntryInfo'>;
invalidateCache?: boolean;
}) { }) {
const { slug: frontmatterSlug } = await contentEntryType.getEntryInfo({ if (!frontmatterSlugCache.has(id) || invalidateCache) {
fileUrl, const { slug: frontmatterSlug } = await contentEntryType.getEntryInfo({
contents: await fs.promises.readFile(fileUrl, 'utf-8'), fileUrl,
}); contents: await fs.promises.readFile(fileUrl, 'utf-8'),
});
frontmatterSlugCache.set(id, frontmatterSlug);
}
const frontmatterSlug = frontmatterSlugCache.get(id);
return parseEntrySlug({ generatedSlug, frontmatterSlug, id, collection }); return parseEntrySlug({ generatedSlug, frontmatterSlug, id, collection });
} }