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

fix route sorting #175

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/core/create_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions test/unit/create_routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down