diff options
author | 2023-06-10 13:49:17 -0400 | |
---|---|---|
committer | 2023-06-10 13:49:17 -0400 | |
commit | 0a8d178c90f033fbba40666c54bcfc58c53ac905 (patch) | |
tree | 4a71eb240bba479d34b8d0d5124aaec4fabf2a71 | |
parent | 777e5d758700802b7497bbd980aea76ca585c6ea (diff) | |
download | astro-0a8d178c90f033fbba40666c54bcfc58c53ac905.tar.gz astro-0a8d178c90f033fbba40666c54bcfc58c53ac905.tar.zst astro-0a8d178c90f033fbba40666c54bcfc58c53ac905.zip |
Raise error for duplicate content entry slugs (#7352)
* chore: throw error on dup slugs
* chore: move to ErrorData
* fix: remove unknownerror
* chore: changeset
-rw-r--r-- | .changeset/yellow-icons-attack.md | 5 | ||||
-rw-r--r-- | packages/astro/src/content/vite-plugin-content-virtual-mod.ts | 10 | ||||
-rw-r--r-- | packages/astro/src/core/errors/errors-data.ts | 12 |
3 files changed, 27 insertions, 0 deletions
diff --git a/.changeset/yellow-icons-attack.md b/.changeset/yellow-icons-attack.md new file mode 100644 index 000000000..93be1fcd9 --- /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 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 bd516b199..33aae964d 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,16 @@ export async function getStringifiedLookupMap({ fileUrl: pathToFileURL(filePath), contentEntryType, }); + if (lookupMap[collection]?.entries?.[slug]) { + throw new AstroError({ + ...AstroErrorData.DuplicateContentEntrySlugError, + message: AstroErrorData.DuplicateContentEntrySlugError.message(collection, slug), + hint: + slug !== generatedSlug + ? `Check the \`slug\` frontmatter property in **${id}**.` + : undefined, + }); + } lookupMap[collection] = { type: 'content', entries: { diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index 17a5db4fe..54dd32fba 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.`; + }, + }, /** * @docs |