From 7771a0aee72d179b7d59b29c57701b8875436ba8 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 9 Jun 2023 16:46:16 -0400 Subject: [PATCH 1/4] chore: throw error on dup slugs --- .../src/content/vite-plugin-content-virtual-mod.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts index bd516b199c82..352244cd5c24 100644 --- a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts +++ b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts @@ -166,6 +166,19 @@ export async function getStringifiedLookupMap({ fileUrl: pathToFileURL(filePath), contentEntryType, }); + if (lookupMap[collection]?.entries?.[slug]) { + throw new AstroError({ + code: 99999, + title: 'Duplicate content entry slug', + message: `Multiple content entries in **${collection}** with slug ${JSON.stringify( + slug + )}. Slugs must be unique.`, + hint: + slug !== generatedSlug + ? `Check the \`slug\` frontmatter property in **${id}**.` + : undefined, + }); + } lookupMap[collection] = { type: 'content', entries: { From 54f14ac7811cc3ba013f0a47b6934a536ef5c865 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 9 Jun 2023 16:49:15 -0400 Subject: [PATCH 2/4] chore: move to ErrorData --- .../src/content/vite-plugin-content-virtual-mod.ts | 5 ++--- packages/astro/src/core/errors/errors-data.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts index 352244cd5c24..110511cf4097 100644 --- a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts +++ b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts @@ -168,11 +168,10 @@ export async function getStringifiedLookupMap({ }); if (lookupMap[collection]?.entries?.[slug]) { throw new AstroError({ + ...AstroErrorData.DuplicateContentEntrySlugError, code: 99999, title: 'Duplicate content entry slug', - message: `Multiple content entries in **${collection}** with slug ${JSON.stringify( - slug - )}. Slugs must be unique.`, + message: AstroErrorData.DuplicateContentEntrySlugError.message(collection, slug), hint: slug !== generatedSlug ? `Check the \`slug\` frontmatter property in **${id}**.` diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index 0425eb22e060..5fa2b4b5c7f1 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -1112,6 +1112,18 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati }, hint: 'Ensure your data entry is an object with valid JSON (for `.json` entries) or YAML (for `.yaml` entries).', }, + /** + * @docs + * @description + * Content collection entries must have unique slugs. Duplicates are often caused by the `slug` frontmatter property. + */ + DuplicateContentEntrySlugError: { + title: 'Duplicate content entry slug.', + code: 9008, + message: (collection: string, slug: string) => { + return `**${collection}** contains multiple entries with the same slug: \`${slug}\`. Slugs must be unique.`; + }, + }, // Generic catch-all - Only use this in extreme cases, like if there was a cosmic ray bit flip UnknownError: { From 6a4849328386776f4a6dae2ba886268832a01c52 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 9 Jun 2023 16:51:15 -0400 Subject: [PATCH 3/4] fix: remove unknownerror --- packages/astro/src/content/vite-plugin-content-virtual-mod.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts index 110511cf4097..33aae964d890 100644 --- a/packages/astro/src/content/vite-plugin-content-virtual-mod.ts +++ b/packages/astro/src/content/vite-plugin-content-virtual-mod.ts @@ -169,8 +169,6 @@ export async function getStringifiedLookupMap({ if (lookupMap[collection]?.entries?.[slug]) { throw new AstroError({ ...AstroErrorData.DuplicateContentEntrySlugError, - code: 99999, - title: 'Duplicate content entry slug', message: AstroErrorData.DuplicateContentEntrySlugError.message(collection, slug), hint: slug !== generatedSlug From bb38e8edc84600a860ab1b9029530ecb2e92a40b Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 9 Jun 2023 16:54:50 -0400 Subject: [PATCH 4/4] chore: changeset --- .changeset/yellow-icons-attack.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/yellow-icons-attack.md diff --git a/.changeset/yellow-icons-attack.md b/.changeset/yellow-icons-attack.md new file mode 100644 index 000000000000..93be1fcd91fb --- /dev/null +++ b/.changeset/yellow-icons-attack.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Raise error when multiple content collection entries have the same slug