@@ -74,30 +74,27 @@ export default function create_routes({ files } = { files: glob.sync('**/*.+(htm
74
74
} )
75
75
. filter ( Boolean )
76
76
. sort ( ( a : Route , b : Route ) => {
77
- let same = true ;
77
+ if ( a . file === '4xx.html' || a . file === '5xx.html' ) return - 1 ;
78
+ if ( b . file === '4xx.html' || b . file === '5xx.html' ) return 1 ;
78
79
79
- for ( let i = 0 ; true ; i += 1 ) {
80
+ const max = Math . max ( a . parts . length , b . parts . length ) ;
81
+
82
+ for ( let i = 0 ; i < max ; i += 1 ) {
80
83
const a_part = a . parts [ i ] ;
81
84
const b_part = b . parts [ i ] ;
82
85
83
- if ( ! a_part && ! b_part ) {
84
- if ( same ) throw new Error ( `The ${ a . file } and ${ b . file } routes clash` ) ;
85
- return 0 ;
86
- }
87
-
88
86
if ( ! a_part ) return - 1 ;
89
87
if ( ! b_part ) return 1 ;
90
88
91
89
const a_sub_parts = get_sub_parts ( a_part ) ;
92
90
const b_sub_parts = get_sub_parts ( b_part ) ;
91
+ const max = Math . max ( a_sub_parts . length , b_sub_parts . length ) ;
93
92
94
- for ( let i = 0 ; true ; i += 1 ) {
93
+ for ( let i = 0 ; i < max ; i += 1 ) {
95
94
const a_sub_part = a_sub_parts [ i ] ;
96
95
const b_sub_part = b_sub_parts [ i ] ;
97
96
98
- if ( ! a_sub_part && ! b_sub_part ) break ;
99
-
100
- if ( ! a_sub_part ) return 1 ; // note this is reversed from above — match [foo].json before [foo]
97
+ if ( ! a_sub_part ) return 1 ; // b is more specific, so goes first
101
98
if ( ! b_sub_part ) return - 1 ;
102
99
103
100
if ( a_sub_part . dynamic !== b_sub_part . dynamic ) {
@@ -109,6 +106,8 @@ export default function create_routes({ files } = { files: glob.sync('**/*.+(htm
109
106
}
110
107
}
111
108
}
109
+
110
+ throw new Error ( `The ${ a . file } and ${ b . file } routes clash` ) ;
112
111
} ) ;
113
112
114
113
return routes ;
0 commit comments