routeComparator() — astro Function Reference
Architecture documentation for the routeComparator() function in priority.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD d9a75617_ba7d_19c7_aba9_3ce175c9b813["routeComparator()"] eb0295fb_0f16_eb12_dcf0_e9ede8e7d690["priority.ts"] d9a75617_ba7d_19c7_aba9_3ce175c9b813 -->|defined in| eb0295fb_0f16_eb12_dcf0_e9ede8e7d690 style d9a75617_ba7d_19c7_aba9_3ce175c9b813 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/astro/src/core/routing/priority.ts lines 24–105
export function routeComparator(a: RouteData, b: RouteData) {
const commonLength = Math.min(a.segments.length, b.segments.length);
for (let index = 0; index < commonLength; index++) {
const aSegment = a.segments[index];
const bSegment = b.segments[index];
const aIsStatic = aSegment.every((part) => !part.dynamic && !part.spread);
const bIsStatic = bSegment.every((part) => !part.dynamic && !part.spread);
if (aIsStatic && bIsStatic) {
// Both segments are static, they are sorted alphabetically if they are different
const aContent = aSegment.map((part) => part.content).join('');
const bContent = bSegment.map((part) => part.content).join('');
if (aContent !== bContent) {
return aContent.localeCompare(bContent);
}
}
// Sort static routes before dynamic routes
if (aIsStatic !== bIsStatic) {
return aIsStatic ? -1 : 1;
}
const aAllDynamic = aSegment.every((part) => part.dynamic);
const bAllDynamic = bSegment.every((part) => part.dynamic);
// Some route might have partial dynamic segments, e.g. game-[title].astro
// These routes should have higher priority against route that have **only** dynamic segments, e.g. [title].astro
if (aAllDynamic !== bAllDynamic) {
return aAllDynamic ? 1 : -1;
}
const aHasSpread = aSegment.some((part) => part.spread);
const bHasSpread = bSegment.some((part) => part.spread);
// Sort dynamic routes with rest parameters after dynamic routes with single parameters
// (also after static, but that is already covered by the previous condition)
if (aHasSpread !== bHasSpread) {
return aHasSpread ? 1 : -1;
}
}
const aLength = a.segments.length;
const bLength = b.segments.length;
if (aLength !== bLength) {
const aEndsInRest = a.segments.at(-1)?.some((part) => part.spread);
const bEndsInRest = b.segments.at(-1)?.some((part) => part.spread);
if (aEndsInRest !== bEndsInRest && Math.abs(aLength - bLength) === 1) {
// If only one of the routes ends in a rest parameter
// and the difference in length is exactly 1
// and the shorter route is the one that ends in a rest parameter
// the shorter route is considered more specific.
// I.e. `/foo` is considered more specific than `/foo/[...bar]`
if (aLength > bLength && aEndsInRest) {
// b: /foo
// a: /foo/[...bar]
return 1;
}
if (bLength > aLength && bEndsInRest) {
// a: /foo
// b: /foo/[...bar]
return -1;
}
}
// Sort routes by length
return aLength > bLength ? -1 : 1;
}
// Sort endpoints before pages
if ((a.type === 'endpoint') !== (b.type === 'endpoint')) {
return a.type === 'endpoint' ? -1 : 1;
}
// Both routes have segments with the same properties
return a.route.localeCompare(b.route);
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does routeComparator() do?
routeComparator() is a function in the astro codebase, defined in packages/astro/src/core/routing/priority.ts.
Where is routeComparator() defined?
routeComparator() is defined in packages/astro/src/core/routing/priority.ts at line 24.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free