Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error for duplicate content entry slugs #7352

Merged
merged 4 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-icons-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Raise error when multiple content collection entries have the same slug
10 changes: 10 additions & 0 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down