Skip to content

Commit

Permalink
fix: avoid parsing slug on unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Jan 23, 2023
1 parent 7299f19 commit 79efd5a
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ContentConfig,
ContentObservable,
ContentPaths,
EntryInfo,
getContentPaths,
getEntryInfo,
getEntrySlug,
Expand Down Expand Up @@ -157,26 +158,19 @@ export async function createContentTypesGenerator({
return { shouldGenerateTypes: false };
}

// `slug` may be present in entry frontmatter.
// This should be respected by the generated `slug` type!
// Parse frontmatter and retrieve `slug` value for this.
// Note: will raise any YAML exceptions and `slug` parse errors (i.e. `slug` is a boolean)
// on dev server startup or production build init.
const rawContents = await fs.promises.readFile(event.entry, 'utf-8');
const { data: frontmatter } = parseFrontmatter(rawContents, fileURLToPath(event.entry));
const slug = getEntrySlug({ ...entryInfo, data: frontmatter });
const { id, collection } = entryInfo;

const collectionKey = JSON.stringify(collection);
const entryKey = JSON.stringify(id);

switch (event.name) {
case 'add':
const addedSlug = await parseSlug({ fs, event, entryInfo });
if (!(collectionKey in contentTypes)) {
addCollection(contentTypes, collectionKey);
}
if (!(entryKey in contentTypes[collectionKey])) {
setEntry(contentTypes, collectionKey, entryKey, slug);
setEntry(contentTypes, collectionKey, entryKey, addedSlug);
}
return { shouldGenerateTypes: true };
case 'unlink':
Expand All @@ -187,8 +181,9 @@ export async function createContentTypesGenerator({
case 'change':
// User may modify `slug` in their frontmatter.
// Only regen types if this change is detected.
if (contentTypes[collectionKey]?.[entryKey]?.slug !== slug) {
setEntry(contentTypes, collectionKey, entryKey, slug);
const changedSlug = await parseSlug({ fs, event, entryInfo });
if (contentTypes[collectionKey]?.[entryKey]?.slug !== changedSlug) {
setEntry(contentTypes, collectionKey, entryKey, changedSlug);
return { shouldGenerateTypes: true };
}
return { shouldGenerateTypes: false };
Expand Down Expand Up @@ -259,6 +254,25 @@ function removeCollection(contentMap: ContentTypes, collectionKey: string) {
delete contentMap[collectionKey];
}

async function parseSlug({
fs,
event,
entryInfo,
}: {
fs: typeof fsMod;
event: ContentEvent;
entryInfo: EntryInfo;
}) {
// `slug` may be present in entry frontmatter.
// This should be respected by the generated `slug` type!
// Parse frontmatter and retrieve `slug` value for this.
// Note: will raise any YAML exceptions and `slug` parse errors (i.e. `slug` is a boolean)
// on dev server startup or production build init.
const rawContents = await fs.promises.readFile(event.entry, 'utf-8');
const { data: frontmatter } = parseFrontmatter(rawContents, fileURLToPath(event.entry));
return getEntrySlug({ ...entryInfo, data: frontmatter });
}

function setEntry(
contentTypes: ContentTypes,
collectionKey: string,
Expand Down

0 comments on commit 79efd5a

Please sign in to comment.