Skip to content

Commit

Permalink
refactor: add cache layer
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Apr 27, 2023
1 parent 7f77913 commit 1c3bfdc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export async function createContentTypesGenerator({
fileUrl: event.entry,
contentEntryType,
fs,
invalidateCache: true,
});
if (!(collectionKey in contentTypes)) {
addCollection(contentTypes, collectionKey);
Expand All @@ -225,6 +226,7 @@ export async function createContentTypesGenerator({
fileUrl: event.entry,
contentEntryType,
fs,
invalidateCache: true,
});
if (contentTypes[collectionKey]?.[entryKey]?.slug !== changedSlug) {
setEntry(contentTypes, collectionKey, entryKey, changedSlug);
Expand Down
15 changes: 11 additions & 4 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ export async function getStringifiedLookupMap({
return JSON.stringify(filePathByLookupId);
}

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

Expand Down

0 comments on commit 1c3bfdc

Please sign in to comment.