diff --git a/src/core/create_routes.ts b/src/core/create_routes.ts index 12d57c7..6215932 100644 --- a/src/core/create_routes.ts +++ b/src/core/create_routes.ts @@ -4,7 +4,7 @@ import { locations } from '../config'; import { Page, PageComponent, ServerRoute } from '../interfaces'; import { posixify } from './utils'; -const fallback_index = posixify(path.resolve( +const fallback_file = posixify(path.resolve( __dirname, '../fallback.html' )); @@ -14,6 +14,11 @@ export default function create_routes(cwd = locations.routes()) { const pages: Page[] = []; const server_routes: ServerRoute[] = []; + const fallback = { + name: 'fallback', + file: path.relative(cwd, fallback_file) + }; + function walk( dir: string, parent_segments: Part[][], @@ -104,17 +109,18 @@ export default function create_routes(cwd = locations.routes()) { } : null; - if (component) components.push(component); + if (component) { + components.push(component); + } else if (components.indexOf(fallback) === -1) { + components.push(fallback); + } walk( path.join(dir, item.basename), segments, params, stack.concat({ - component: component || { - name: 'fallback', - file: fallback_index - }, + component: component || fallback, params }) ); diff --git a/test/app/routes/missing-index/ok.html b/test/app/routes/missing-index/ok.html new file mode 100644 index 0000000..7cc8f66 --- /dev/null +++ b/test/app/routes/missing-index/ok.html @@ -0,0 +1 @@ +

it works

\ No newline at end of file diff --git a/test/common/test.js b/test/common/test.js index fa71158..a9b0caf 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -659,6 +659,14 @@ function run({ mode, basepath = '' }) { ]); }); }); + + it('uses a fallback index component if none is provided', () => { + return nightmare.goto(`${base}/missing-index/ok`) + .page.title() + .then(title => { + assert.equal(title, 'it works'); + }); + }); }); describe('headers', () => {