-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Implement priority overrides for injected routes and redirects #9439
Changes from 11 commits
9f6a7e9
a6671a8
bf8a333
31885a9
06bcec2
8a61bb7
a475688
c211704
0c678b2
7379e1d
a064b4e
cea5439
9dd4609
addd2d9
e3cbbe9
d5e2142
322bb4d
28df92f
a368d60
dc2760a
7cfbdbd
61fc289
eb855b9
a8686b8
e319eb4
bd93047
de3a8cb
fdc7c90
5671f41
24c9c74
7fc2482
30241b6
fb78e03
dcf8ca4
9882de1
a34647e
dd8c15f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Reworks route priority processing to allow for more flexible and intuitive redirects and route injection | ||
|
||
- Priority order for project routes, injected routes and redirects are now all the same. | ||
- Injected routes and redirects can now specify if they should be prioritized above, | ||
with or below project routes. This is done by adding a `priority` property to the route | ||
object in `injectRoute` and in the `redirects` property in `astro.config.mjs`. | ||
- Now more specific routes always take priority over less specific routes. | ||
Example: `/blog/[...slug]` will take priority over `/[...slug]` | ||
- Static redirects now have a lower priority than all project routed, even if the routes are dynamic, | ||
matching the already documented behavior. | ||
Example: `/blog (redirect)` will no longer override a `/[slug]` route by default, this can be re-enabled | ||
using the new `priority` field. | ||
- Collision detection between routes can now detect coliisions between similar dynamic routes | ||
of any kind (project routes, injected routes and redirects). | ||
Example: `/blog/[page]` will now be detected as a collision with `/blog/[slug]` | ||
- Colision detection is now reported as a warning for backward compatibility with all the previous false negatives. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1555,10 +1555,22 @@ export interface AstroUserConfig { | |
*/ | ||
export type InjectedScriptStage = 'before-hydration' | 'head-inline' | 'page' | 'page-ssr'; | ||
|
||
/** | ||
* IDs for different priorities of injected routes and redirects: | ||
Fryuni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* - "override": Override any file-based project route in case of conflict. Conflicts within override | ||
* routes are resolved by the same rules as file-based routes. | ||
* - "merge": Merge with discovered file-based project routes, behaving the same as if the route | ||
* was defined as a file in the project. | ||
* - "defer": Defer to any file-based project route in case of conflict. Conflicts within defer | ||
* routes are resolved by the same rules as file-based routes. | ||
*/ | ||
export type RoutePriorityOverride = 'override' | 'normal' | 'defer'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the plan for this to be user-configurable @Fryuni ? If so, where/how? If in Also reminder that you will also need an accompanying PR to update: https://docs.astro.build/en/core-concepts/routing/#route-priority-order (FYI It's OK if this update doesn't entirely duplicate the config option, but instead just links to it, if you're also adding it to config reference. It might be most helpful if this sections lists out the default, then links to the configurable option for "fine-tuning") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On redirects this is user-configurable, on routes this is configurable for integrations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think injected routes should have the ability to decide how they should be prioritized. I think the only options should be "normal" (ie, the same as if they were file-based routes) and maybe something called "legacy" (the old behavior, only for backwards compatibility, to be removed in next major). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the customization is positive for integrations. For example, Starlight injects a 404 route. With this change it could be injected with Also, if we change to have a "normal" and "legacy", I'd expect the "normal" to be the default. But that would be a breaking change. I'm not sure how much of an impact this would be on integrations in the wild, but IMHO it would require a major version by itself. It wasn't something documents though, so I'm not too strongly of that opinion. That was the implemented behavior but not necessarily intended. So maybe it is fine to break integrations that rely on it. I don't know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's a fact, and I agree with your point. However, we risk not providing predictability to the end users, who need to learn what integrations do. While it's OK to change. In the prioritization logic, we also make sure that the user is protected from what integrations do. I believe we can progressively provide integrations more control, although we should first fix the predictability of the prioritization. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Fryuni It should be possible for Starlight to define 404 via middleware. I think a lot of what Starlight does might be better via middleware. Currently middleware is a little limited and can't do something like 404, we are in the process of fixing that though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rethinking about this, if an integration wants to provide a route that can be overridden by the project it is on, it could just allow disabling the route. If Starlight wants to allow overriding the 404 page it could have a Considering that I don't see much advantage of allowing these different priorities. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was hoping to avoid exactly that kind of option with the priority system. UX with a
UX with a lower route priority when injecting Starlight’s 404:
|
||
|
||
export interface InjectedRoute { | ||
pattern: string; | ||
entrypoint: string; | ||
prerender?: boolean; | ||
priority?: RoutePriorityOverride; | ||
} | ||
|
||
export interface ResolvedInjectedRoute extends InjectedRoute { | ||
|
@@ -2385,6 +2397,7 @@ type RedirectConfig = | |
| { | ||
status: ValidRedirectStatus; | ||
destination: string; | ||
priority?: RoutePriorityOverride; | ||
}; | ||
|
||
export interface RouteData { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know when this is considered finalized behaviour/naming etc, and please
/ptal
me for a review at that time!