-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement priority overrides for injected routes and redirects (#9439)
* Implement priority overrides for injected routes and redirects * Fix ordering for route specificity * Don't mix rules on tests * Detailed collision detection * Add changeset * Remove TODO * Add comments to clarify default values * Update terminology * Revert unrelated changes * WIP * Refactor * Fix typo and typing * chore: default to legacy * chore: use experimental flag instead of option * fix: do not throw an error on collisions * chore: fix regression * chore: use `continue` instead of `return` * chore: fix tests but one * chore: Update test * chore: Change remaining new error to warning * chore: Test collision warnings * docs: Update docs of new config * docs: Improve changesets * chore: rename experimental flag * chore: update changeset and docs * Sarah editing pass * nit: Align Markdown table * defined definitions! Co-authored-by: Luiz Ferraz <luiz@lferraz.com> * added logging info to docs for experimental flag * Yan final boss review Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com> * chore: Update flag name in tests * chore: Update flag name in tests --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
- Loading branch information
1 parent
efadef2
commit fd17f4a
Showing
6 changed files
with
755 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
Updates [Astro's routing priority rules](https://docs.astro.build/en/core-concepts/routing/#route-priority-order) to prioritize the most specifically-defined routes. | ||
|
||
Now, routes with **more defined path segments** will take precedence over less specific routes. | ||
|
||
For example, `/blog/posts/[pid].astro` (3 path segments) takes precedence over `/blog/[...slug].astro` (2 path segments). This means that: | ||
|
||
- `/pages/blog/posts/[id].astro` will build routes of the form `/blog/posts/1` and `/blog/posts/a` | ||
- `/pages/blog/[...slug].astro` will build routes of a variety of forms, including `blog/1` and `/blog/posts/1/a`, but will not build either of the previous routes. | ||
|
||
For a complete list of Astro's routing priority rules, please see the [routing guide](https://docs.astro.build/en/core-concepts/routing/#route-priority-order). This should not be a breaking change, but you may wish to inspect your built routes to ensure that your project is unaffected. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Adds an experimental flag `globalRoutePriority` to prioritize redirects and injected routes equally alongside file-based project routes, following the same [route priority order rules](https://docs.astro.build/en/core-concepts/routing/#route-priority-order) for all routes. | ||
|
||
```js | ||
// astro.config.mjs | ||
export default defineConfig({ | ||
experimental: { | ||
globalRoutePriority: true, | ||
}, | ||
}) | ||
``` | ||
|
||
Enabling this feature ensures that all routes in your project follow the same, predictable route priority order rules. In particular, this avoids an issue where redirects or injected routes (e.g. from an integration) would always take precedence over local route definitions, making it impossible to override some routes locally. | ||
|
||
The following table shows which route builds certain page URLs when file-based routes, injected routes, and redirects are combined as shown below: | ||
|
||
- File-based route: `/blog/post/[pid]` | ||
- File-based route: `/[page]` | ||
- Injected route: `/blog/[...slug]` | ||
- Redirect: `/blog/tags/[tag]` -> `/[tag]` | ||
- Redirect: `/posts` -> `/blog` | ||
|
||
URLs are handled by the following routes: | ||
|
||
| Page | Current Behavior | Global Routing Priority Behavior | | ||
|--------------------|----------------------------------|-------------------------------------| | ||
| `/blog/tags/astro` | Injected route `/blog/[...slug]` | Redirect to `/tags/[tag]` | | ||
| `/blog/post/0` | Injected route `/blog/[...slug]` | File-based route `/blog/post/[pid]` | | ||
| `/posts` | File-based route `/[page]` | Redirect to `/blog` | | ||
|
||
In the event of route collisions, where two routes of equal route priority attempt to build the same URL, Astro will log a warning identifying the conflicting routes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.