Do a simple push for priority

This commit is contained in:
Matthew Phillips 2023-05-30 18:33:43 -04:00
parent d3895a2d71
commit 2700c125c9
2 changed files with 6 additions and 36 deletions

View file

@ -228,30 +228,6 @@ function areSiblings(a: RouteData, b: RouteData) {
return true;
}
// A fast insertion method based on binary search.
function binaryInsert<T>(sorted: T[], item: T, insertComparator: (a: T, item: T) => boolean) {
if(sorted.length === 0) {
sorted.push(item);
return 0;
}
let low = 0, high = sorted.length - 1, mid = 0;
while (low <= high) {
mid = low + (high - low >> 1);
if(insertComparator(sorted[mid], item)) {
low = mid + 1;
} else {
high = mid -1;
}
}
if(insertComparator(sorted[mid], item)) {
mid++;
}
sorted.splice(mid, 0, item);
return mid;
}
export interface CreateRouteManifestParams {
/** Astro Settings object */
settings: AstroSettings;
@ -505,17 +481,9 @@ export function createRouteManifest(
redirect: to,
redirectRoute: routes.find(r => r.route === to)
};
const isSpreadRoute = isSpread(route);
binaryInsert(routes, routeData, (a, item) => {
// If the routes are siblings and the redirect route is a spread
// Then it should come after the sibling unless it is also a spread.
// This essentially means that redirects are prioritized when *exactly* the same.
if(isSpreadRoute && areSiblings(a, item)) {
return !isSpread(a.route);
}
return true;
});
// Push so that redirects are selected last.
routes.push(routeData);
});
return {

View file

@ -45,7 +45,8 @@ describe('routing - createRouteManifest', () => {
base: '/search',
trailingSlash: 'never',
redirects: {
'/blog/[...slug]': '/'
'/blog/[...slug]': '/',
'/blog/contributing': '/another',
}
},
root
@ -57,6 +58,7 @@ describe('routing - createRouteManifest', () => {
});
expect(manifest.routes[1].route).to.equal('/blog/contributing');
expect(manifest.routes[1].type).to.equal('page');
expect(manifest.routes[2].route).to.equal('/blog/[...slug]');
})
});