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

Conversation

bholmesdev
Copy link
Contributor

Changes

  • Resolves Rendered <Content/> from collections is not translating (astro-i18n) #7133 - this issue discovered that you can create duplicate content entry slugs with the slug frontmatter property. This silently maps multiple entries to the same identifier, breaking getEntryBySlug() and our internal rendering logic.
  • Raise an error when this is detected. There may be good reasons to allow multiple entries with the same slug. But today, this is difficult to support in a non-breaking way.
image

Testing

Manually test that error is readable

Docs

New AstroErrorsData entry

@bholmesdev bholmesdev requested a review from a team as a code owner June 9, 2023 20:54
@changeset-bot
Copy link

changeset-bot bot commented Jun 9, 2023

🦋 Changeset detected

Latest commit: bb38e8e

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jun 9, 2023
@bholmesdev bholmesdev merged commit 0a8d178 into main Jun 10, 2023
@bholmesdev bholmesdev deleted the fix/astro-i18n-content-collections branch June 10, 2023 17:49
@astrobot-houston astrobot-houston mentioned this pull request Jun 10, 2023
@eybarta
Copy link

eybarta commented Jan 9, 2024

Hi,

I'm having trouble with setting up slugs for multiple language blog posts, since I can't use duplicate slugs, how do I name the slugs across languages? i.e

english: "blog-post-1"
spanish: "blog-post-1"
^^ This throws the duplicate slug error

english: "/en/blog-post-1"
spanish: "/es/blog-post-1"
^^ This throws the rgx error not accepting "/"
image

The last example seems to be same as in documentation.. maybe there's an issue? or please clarify what's the correct way to implement this. 🙏

@bholmesdev
Copy link
Contributor Author

Hey @eybarta! That second error is interesting. I wonder if that error is coming from our router rather than content collections. Are you using the dynamic pattern [...slug].astro for the filename? If you used [slug].astro instead (without the ...), the nested slug will fail. Sorry the error message is unclear

@eybarta
Copy link

eybarta commented Jan 9, 2024

@bholmesdev

Yep, I was using [slug].astro, will try to change [...slug].astro

Thanks for your help 🙏

@eybarta
Copy link

eybarta commented Jan 10, 2024

Sorry I'm still having some issues, everything seems to work fine but in my [...slug].astro file
inside getStaticPaths()
I'm doing:
const pages = await getCollection('blog');

But this populates 'pages' with blog posts from all languages. My file structure is:
/content/blog/en/...
/content/blog/es/...

I tried filtering the pages to include only posts from the current language, but I don't have access to Astro.url
inside of getStaticPaths(). GPT suggested to use /content/en-blog/, is this the recommended way to solve this issue?
or is there a way to keep the file structure similar except for the /en/ lang part? and have getCollection() fetch only the pages for the current language?
(I found this issue because it was rendering the blog post for the wrong language...)

Appreciate your help on this :)

@Rigel772
Copy link

@eybarta
You can add a filter to getCollection() function
To get english posts (from "en" folder) you can use:
const postsEn = await getCollection("blog", p => p.id.startsWith("en/"))
getCollection returns id which contains en/ path for content in that folder
You can use the filter to check other parameters, eg isPublic from the frontmatter
const postsEn = await getCollection("blog", p => p.id.startsWith("en/") && p.data.isPublic)

@eybarta
Copy link

eybarta commented Jan 18, 2024

Sweet! thank you, didn't know I can do that.
Is there a reference in the docs for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rendered <Content/> from collections is not translating (astro-i18n)
4 participants