#Slugg

3 messages · Page 1 of 1 (latest)

orchid wolf
#

Can I have multiple slugs pointing to the same page

agile pollenBOT
#

Hi @orchid wolf. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

No, you cannot have multiple slugs pointing to the same page in Astro. According to the Astro documentation (https://docs.astro.build/en/reference/errors/duplicate-content-entry-slug-error/#_top), content collection entries must have unique slugs. Duplicates are often caused by the slug frontmatter property. If a collection contains multiple entries with the same slug, an error will be thrown.

This is because the slug is used to generate the URL for the specific page. Having multiple entries with the same slug would cause confusion as to which page should be rendered when the URL is accessed.

If you want to create multiple pages from a single src/pages/ component, you can use the getStaticPaths() function to create your new URL paths using the slug property of each content entry. This will generate a new page for every entry in the collection. For example, an entry at src/content/blog/hello-world.md will have a slug of hello-world, and therefore its final URL will be /posts/hello-world/.

Here is an example of how you can use getStaticPaths() to generate multiple pages:

---import { getCollection } from 'astro:content';// 1. Generate a new path for every collection entryexport async function getStaticPaths() {  const blogEntries = await getCollection('blog');  return blogEntries.map(entry => ({    params: { slug: entry.slug }, props: { entry },  }));}// 2. For your template, you can get the entry directly from the propconst { entry } = Astro.props;const { Content } = await entry.render();---<h1>{entry.data.title}</h1><Content />

(Source: Astro Documentation (https://docs.astro.build/en/guides/content-collections/#building-for-static-output-default))

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: