#help with dynamic route with [slug].astro

1 messages · Page 1 of 1 (latest)

paper wing
#

Hello everyone, I need help:
I'm coding astro whose job is to view post details. In the post list screen, I have an a tag to redirect with the slug extension (For example: /post/8). So in the file [slug].astro I have to declare getStaticPaths() but how can I predict the params, because the other slug is arbitrary and not fixed. How do I not have to define it first? Thanks and please help me

edgy prawn
#

getStaticPaths() generates pages for all known routes in "static" mode (i.e. SSG)
If you're routes are not known at build time, then you should use "hybrid" or "server" mode (i.e. SSR).

On the other hand, if you are trying to say that /post/8 should go to page 8 of the post list, then you are talking about pagination.
https://docs.astro.build/en/guides/routing/#pagination

Docs

An intro to routing with Astro.

paper wing
edgy prawn
#

getStaticPaths() does not "get" a slug, it's a static page generator. It generates all pages for all the slugs at build time. So when a visitor goes to /news/story1, getStaticPaths() has already generated that page.

You should consider starting from the beginning with the "Build your first Astro Blog" tutorial:
https://docs.astro.build/en/tutorial/0-introduction/
Or if you prefer watching videos, try:
https://www.youtube.com/watch?v=F2pw1C9eKXw

Docs

Learn the basics of Astro with a project-based tutorial. All the background knowledge you need to get started!

Join early access for my Astro course! https://learnastro.dev

In this course, we’ll build a custom Astro blog from scratch. Throughout the course, we’ll learn how to work with Astro components, import and process data, pass props, minify images, build an RSS feed, and more.

🔗 Key Links 🔗

▶ Play video