Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document counter-intuitive difference for dynamic routes on SSR and SSG #10468

Open
Fryuni opened this issue Dec 19, 2024 · 3 comments
Open

Document counter-intuitive difference for dynamic routes on SSR and SSG #10468

Fryuni opened this issue Dec 19, 2024 · 3 comments
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)

Comments

@Fryuni
Copy link
Member

Fryuni commented Dec 19, 2024

Just a small little detail about routing on this example:

/src/pages
└── [...locale]/
    └── [...slug].astro

That would not work as expected in on-demand pages (SSR). Rest parameters ([...name]) can match any number of path segments and as a consequence using two rest parameters in the same routes makes parsing the path ambiguous. For example, a request to /en/blog/amazing-article could be parsed in any of the following ways:

  • {locale: undefined, slug: 'en/blog/amazing-article'}
  • {locale: 'en', slug: 'blog/amazing-article'}
  • {locale: 'en/blog', slug: 'amazing-article'}
  • {locale: 'en/blog/amazing-article', slug: undefined}

It can be used to generate the page, so for prerendered pages, this is fine since the routes are generated from getStaticPaths. Astro provides no guarantees about which of those interpretations will be used during parsing. We use a library to turn the file path into a parsing function, which also doesn't guarantee the behavior when facing this ambiguity (in fact, the behavior changed during a patch release earlier this year).

So if you do want to have an optional locale in the URL and use SSR the recommendation would be to have the entire tree twice, once on src/pages and once on src/pages/[locale]

Originally posted by @Fryuni in #9256 (comment)

@sarah11918 sarah11918 added improve documentation Enhance existing documentation (e.g. add an example, improve description) help wanted Issues looking for someone to run with them! labels Dec 19, 2024
@sarah11918
Copy link
Member

So the issue here is that https://docs.astro.build/en/guides/routing/#server-ssr-mode says "routing works the same as for SSG" when it doesn't exactly work the same, and you can't have two parameters.

@delucis
Copy link
Member

delucis commented Dec 21, 2024

You can have two parameters if they’re not using the ... spread syntax.

src/pages/[locale]/[...slug].astro or src/pages/[locale]/[slug].astro would both be fine.

@Fryuni
Copy link
Member Author

Fryuni commented Dec 22, 2024

Yeah, this is specifically about having two rest parameters ([...locale]/[...slug]). Which was recommended as a workaround on that issue and multiple times over Discord.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

No branches or pull requests

3 participants