Merge pull request #175 from sveltejs/fix-route-sorting

fix route sorting
This commit is contained in:
Rich Harris
2018-03-07 14:32:56 -05:00
committed by GitHub
2 changed files with 45 additions and 11 deletions

View File

@@ -74,30 +74,27 @@ export default function create_routes({ files } = { files: glob.sync('**/*.+(htm
})
.filter(Boolean)
.sort((a: Route, b: Route) => {
let same = true;
if (a.file === '4xx.html' || a.file === '5xx.html') return -1;
if (b.file === '4xx.html' || b.file === '5xx.html') return 1;
for (let i = 0; true; i += 1) {
const max = Math.max(a.parts.length, b.parts.length);
for (let i = 0; i < max; i += 1) {
const a_part = a.parts[i];
const b_part = b.parts[i];
if (!a_part && !b_part) {
if (same) throw new Error(`The ${a.file} and ${b.file} routes clash`);
return 0;
}
if (!a_part) return -1;
if (!b_part) return 1;
const a_sub_parts = get_sub_parts(a_part);
const b_sub_parts = get_sub_parts(b_part);
const max = Math.max(a_sub_parts.length, b_sub_parts.length);
for (let i = 0; true; i += 1) {
for (let i = 0; i < max; i += 1) {
const a_sub_part = a_sub_parts[i];
const b_sub_part = b_sub_parts[i];
if (!a_sub_part && !b_sub_part) break;
if (!a_sub_part) return 1; // note this is reversed from above — match [foo].json before [foo]
if (!a_sub_part) return 1; // b is more specific, so goes first
if (!b_sub_part) return -1;
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
@@ -109,6 +106,8 @@ export default function create_routes({ files } = { files: glob.sync('**/*.+(htm
}
}
}
throw new Error(`The ${a.file} and ${b.file} routes clash`);
});
return routes;

View File

@@ -22,6 +22,41 @@ describe('create_routes', () => {
);
});
it('prefers index page to nested route', () => {
const routes = create_routes({
files: [
'api/examples/[slug].js',
'api/examples/index.js',
'blog/[slug].html',
'api/gists/[id].js',
'api/gists/index.js',
'4xx.html',
'5xx.html',
'blog/index.html',
'blog/rss.xml.js',
'guide/index.html',
'index.html'
]
});
assert.deepEqual(
routes.map(r => r.file),
[
'4xx.html',
'5xx.html',
'index.html',
'guide/index.html',
'blog/index.html',
'blog/rss.xml.js',
'blog/[slug].html',
'api/examples/index.js',
'api/examples/[slug].js',
'api/gists/index.js',
'api/gists/[id].js',
]
);
});
it('generates params', () => {
const routes = create_routes({
files: ['index.html', 'about.html', '[wildcard].html', 'post/[id].html']