#multiple dynamic routes SSR

5 messages · Page 1 of 1 (latest)

turbid ivy
#

Hello, I've been using SSR for a project lately, and I've had trouble understanding how multiple routes can coexist in the same folder.

Basically I have the following tree structure
articles/
-> [page].astro (which directs to an "all articles" page with pagination)
-> [...slug].astro (which directs to an article)

And basically, the issue I have is, how do I tell my renderer to match with page or slug ? How does Astro which .astro file to pick ? Is there a way inside [page].astro to write a matcher so that if params.page is not a number, then the request should be passed to [...slug].astro ?
Thanks

turbid ivy
#

I can't find anyone online dealing with that issue, or maybe I can't find the right words to search online

obsidian terrace
#

@rocky cave

rocky cave
#

Astro doesn't have such a feature. On SSR it will use the first route that matches the request according to the routing priority rules and use that. If the page returns a 404, them a 404 it is.

What you can do is have a single [...slug] route, no [page] route, and decide dynamically what to render:

---
import AllArticlesPage from './_parts/AllArticlesPage.astro';
import ArticlePage from './_parts/ArticlePage.astro';

const { slug } = Astro.params;
const slugAsPage = Number.parseInt(slug);
---
{Number.isNaN(slugAsPage) && <ArticlePage {slug}/>}
{!Number.isNaN(slugAsPage) && <AllArticlesPage page={slugAsPage}/>}
#

I think it has been discussed at some point here on discord to have a way to defer to the next matching route but I don't remember anything more concrete about it.