Skip to content

Commit 1c3bfdc

Browse files
committed
refactor: add cache layer
1 parent 7f77913 commit 1c3bfdc

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

packages/astro/src/content/types-generator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export async function createContentTypesGenerator({
202202
fileUrl: event.entry,
203203
contentEntryType,
204204
fs,
205+
invalidateCache: true,
205206
});
206207
if (!(collectionKey in contentTypes)) {
207208
addCollection(contentTypes, collectionKey);
@@ -225,6 +226,7 @@ export async function createContentTypesGenerator({
225226
fileUrl: event.entry,
226227
contentEntryType,
227228
fs,
229+
invalidateCache: true,
228230
});
229231
if (contentTypes[collectionKey]?.[entryKey]?.slug !== changedSlug) {
230232
setEntry(contentTypes, collectionKey, entryKey, changedSlug);

packages/astro/src/content/utils.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ export async function getStringifiedLookupMap({
442442
return JSON.stringify(filePathByLookupId);
443443
}
444444

445+
const frontmatterSlugCache = new Map<string, string>();
445446
/**
446447
* Check for slug in content entry frontmatter and validate the type,
447448
* falling back to the `generatedSlug` if none is found.
@@ -453,18 +454,24 @@ export async function getEntrySlug({
453454
contentEntryType,
454455
fileUrl,
455456
fs,
457+
invalidateCache = false,
456458
}: {
457459
fs: typeof fsMod;
458460
id: string;
459461
collection: string;
460462
generatedSlug: string;
461463
fileUrl: URL;
462464
contentEntryType: Pick<ContentEntryType, 'getEntryInfo'>;
465+
invalidateCache?: boolean;
463466
}) {
464-
const { slug: frontmatterSlug } = await contentEntryType.getEntryInfo({
465-
fileUrl,
466-
contents: await fs.promises.readFile(fileUrl, 'utf-8'),
467-
});
467+
if (!frontmatterSlugCache.has(id) || invalidateCache) {
468+
const { slug: frontmatterSlug } = await contentEntryType.getEntryInfo({
469+
fileUrl,
470+
contents: await fs.promises.readFile(fileUrl, 'utf-8'),
471+
});
472+
frontmatterSlugCache.set(id, frontmatterSlug);
473+
}
474+
const frontmatterSlug = frontmatterSlugCache.get(id);
468475
return parseEntrySlug({ generatedSlug, frontmatterSlug, id, collection });
469476
}
470477

0 commit comments

Comments
 (0)