Rest parameter dynamic routes
🌱 This post is in the growth phase. It may still be useful as it grows up.
These examples help me make sense of it.
A file named
[...thing].astro (placed in the root) will match all of these paths:
When nested in a static directory (
[nested]), the rest parameter file matches infinitely nested paths inside that subdirectory path.
When nested in a dynamic directory (
nested), the rest parameter file matches infinitely nested paths for any subdirectory path.
But this only works for pages that
getStaticPaths() has generated a first segment for.
In the example above, the first segments are
To match all the paths above, the resulting
getStaticPaths() needs to look like this:
Note how the entire unmatched path is passed as
Dynamic and static routes can be mixed and matched. I love this example from the Astro docs.
Weird things happen you attempt two dynamic filenames with rest parameters.
Dynamic routes don’t have to exhaustive. You can waste a lot of good effort trying to make your dynamic routes inclusive of your url paths. Many times you can just add more static paths, or fork dynamic paths.
This is the file structure I use to render the lessons, lesson collections, and collection years on this site.