My Fifth Blog Post

8/8/2022

Dynamically routing pages across the sta-I mean, in Astro!

Written by: Astro Learner

The word astro against an illustration of planets and stars.

astro

successes

learning in public

You can create entire sets of pages dynamically using .astro files that export a getStaticPaths() function.

The getStaticPaths() function returns an array of page routes, and all of the pages at those routes will use the same template defined in the file.

With a .astro file named [tag].astro in the src/pages/tags/ directory, Astro will create a page for each tag that you have in your blog posts. The URL for each page will be /tags/<tag>, where <tag> is the name of the tag.

But that’s not all! You can create dynamic routes with multiple parameters, too. For example, a file named [author]/[slug].astro in the src/pages/ directory would create pages at URLs like /jane/my-first-post and /john/my-second-post.

You can read more about dynamic page routing in the Astro documentation.

Use props in dynamic routes

Use import.meta.glob() to import all of your blog posts, and then pass them as props to your dynamic route pages.

You can then filter the list of posts, using Astro’s built-in TypeScript support, based on the route parameters (e.g., the tag name), and display only the posts that match the current route (i.e., the tag specified in the URL).

A getStaticPaths function should always return a list of objects containing params (what to call each page route) and optionally any props (data that you want to pass to those pages).

Wrapping up

If you need information to construct the page routes, write it inside getStaticPaths().

To receive information in the HTML template of a page route, write it outside getStaticPaths().

Be back soon with more adventures across the Astro and beyond!