1

I'm having trouble with dynamic routing in Astro. I'm working with a Content Collection called "Articles" and then created a route: [slug].astro. However, the slug does not resolve to the markdown content, and every time I've run my dev server it resolves to a 404 page, despite my code editor validating that the data has been imported from my Collection folder into the "pages" subdirectory.

From my content/config.ts file:

const articles = defineCollection ({
  type: "content",
  schema: ( {image} ) => z.object({
    title: z.string(),
    category: reference("sections"),
    cover: image(),
    coverAlt: z.string().optional(),
    description: z.string(),
    date: z.date(),
    identifier: z.number(),
    seaslug: z.string().optional(),
  })
})



export const collections = {
  articles,
}

From my articles/[slug].astro file:

---
import { getEntry, getCollection } from "astro:content";

export async function getStaticPaths() {
  const articles = await getCollection('articles');
  return articles.map((p) => ({
    params: { slug: p.slug },
  }));
}

const { slug } = Astro.params;

const articles = await getEntry("articles", slug)

const { Content } = await articles.render();

---

<h1>{articles.data.title}</h1>
<Content />
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.