Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 06af8e8

Browse files
authored
Merge pull request #175 from sveltejs/fix-route-sorting
fix route sorting
2 parents b5a8d29 + 8bb0999 commit 06af8e8

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/core/create_routes.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,27 @@ export default function create_routes({ files } = { files: glob.sync('**/*.+(htm
7474
})
7575
.filter(Boolean)
7676
.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;
7879

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) {
8083
const a_part = a.parts[i];
8184
const b_part = b.parts[i];
8285

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-
8886
if (!a_part) return -1;
8987
if (!b_part) return 1;
9088

9189
const a_sub_parts = get_sub_parts(a_part);
9290
const b_sub_parts = get_sub_parts(b_part);
91+
const max = Math.max(a_sub_parts.length, b_sub_parts.length);
9392

94-
for (let i = 0; true; i += 1) {
93+
for (let i = 0; i < max; i += 1) {
9594
const a_sub_part = a_sub_parts[i];
9695
const b_sub_part = b_sub_parts[i];
9796

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
10198
if (!b_sub_part) return -1;
10299

103100
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
@@ -109,6 +106,8 @@ export default function create_routes({ files } = { files: glob.sync('**/*.+(htm
109106
}
110107
}
111108
}
109+
110+
throw new Error(`The ${a.file} and ${b.file} routes clash`);
112111
});
113112

114113
return routes;

test/unit/create_routes.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,41 @@ describe('create_routes', () => {
2222
);
2323
});
2424

25+
it('prefers index page to nested route', () => {
26+
const routes = create_routes({
27+
files: [
28+
'api/examples/[slug].js',
29+
'api/examples/index.js',
30+
'blog/[slug].html',
31+
'api/gists/[id].js',
32+
'api/gists/index.js',
33+
'4xx.html',
34+
'5xx.html',
35+
'blog/index.html',
36+
'blog/rss.xml.js',
37+
'guide/index.html',
38+
'index.html'
39+
]
40+
});
41+
42+
assert.deepEqual(
43+
routes.map(r => r.file),
44+
[
45+
'4xx.html',
46+
'5xx.html',
47+
'index.html',
48+
'guide/index.html',
49+
'blog/index.html',
50+
'blog/rss.xml.js',
51+
'blog/[slug].html',
52+
'api/examples/index.js',
53+
'api/examples/[slug].js',
54+
'api/gists/index.js',
55+
'api/gists/[id].js',
56+
]
57+
);
58+
});
59+
2560
it('generates params', () => {
2661
const routes = create_routes({
2762
files: ['index.html', 'about.html', '[wildcard].html', 'post/[id].html']

0 commit comments

Comments
 (0)