Do a simple push for priority
This commit is contained in:
parent
d3895a2d71
commit
2700c125c9
2 changed files with 6 additions and 36 deletions
|
@ -228,30 +228,6 @@ function areSiblings(a: RouteData, b: RouteData) {
|
||||||
return true;
|
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 {
|
export interface CreateRouteManifestParams {
|
||||||
/** Astro Settings object */
|
/** Astro Settings object */
|
||||||
settings: AstroSettings;
|
settings: AstroSettings;
|
||||||
|
@ -505,17 +481,9 @@ export function createRouteManifest(
|
||||||
redirect: to,
|
redirect: to,
|
||||||
redirectRoute: routes.find(r => r.route === to)
|
redirectRoute: routes.find(r => r.route === to)
|
||||||
};
|
};
|
||||||
const isSpreadRoute = isSpread(route);
|
|
||||||
|
|
||||||
binaryInsert(routes, routeData, (a, item) => {
|
// Push so that redirects are selected last.
|
||||||
// If the routes are siblings and the redirect route is a spread
|
routes.push(routeData);
|
||||||
// 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;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -45,7 +45,8 @@ describe('routing - createRouteManifest', () => {
|
||||||
base: '/search',
|
base: '/search',
|
||||||
trailingSlash: 'never',
|
trailingSlash: 'never',
|
||||||
redirects: {
|
redirects: {
|
||||||
'/blog/[...slug]': '/'
|
'/blog/[...slug]': '/',
|
||||||
|
'/blog/contributing': '/another',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
root
|
root
|
||||||
|
@ -57,6 +58,7 @@ describe('routing - createRouteManifest', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(manifest.routes[1].route).to.equal('/blog/contributing');
|
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]');
|
expect(manifest.routes[2].route).to.equal('/blog/[...slug]');
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue