Perhaps we want to organize our content file structure differently, or we don’t want to have to keep track of how we name files, or we don’t want to type a post title in two different ways. Each of these situations merit considering a post file naming scheme besides the Astro default.
For example, suppose I have a blog post named “My First Blog Post”. Using the blog starter template, I would create a /src/content/blog/my-first-blog-post.md, and then enter the title in the frontmatter. The title would be used in the header of the post, but its route would be determined by the file name as stored in post.data.id. For my first several posts, I want to organize them in the file structure, maybe, in a subdirectory so that they’re grouped by project, but I don’t want this structure publicly exposed. So perhaps my file name is /src/content/blog/first-project/01.md, but I still want the route to be /2024-12-10-my-first-blog-post/.
To achieve this, we make use of a tool to convert the title into URL format: npm install slugify. This should make it easier to write a helper function to convert post metadata to a slug.
To generate dynamic routes in Astro, we need to use GetStaticPaths to map out all the routes according to a pattern. In this case, since we want the route to end in a slug, we first create the page /src/pages/[slug].astro. The word in between the square brackets can be used as a parameter name.