From 5ddc394a50412f763b5cd6026a4dcc7bf3992554 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Mon, 13 Aug 2018 17:27:20 +0100 Subject: [PATCH] fix: return 404 if extendRoutes without Component (#3608) resolve #3351 --- lib/app/utils.js | 11 ++++++++--- test/fixtures/with-config/nuxt.config.js | 3 +++ test/unit/with-config.test.js | 5 +++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/app/utils.js b/lib/app/utils.js index 49ca3577c9..4f2d4a096e 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -77,9 +77,14 @@ export function getMatchedComponentsInstances(route, matches = false) { export function flatMapComponents(route, fn) { return Array.prototype.concat.apply([], route.matched.map(function (m, index) { - return Object.keys(m.components).map(function (key) { - return fn(m.components[key], m.instances[key], m, key, index) - }) + return Object.keys(m.components).reduce(function (promises, key) { + if (m.components[key]) { + promises.push(fn(m.components[key], m.instances[key], m, key, index)) + } else { + delete m.components[key] + } + return promises + }, []) })) } diff --git a/test/fixtures/with-config/nuxt.config.js b/test/fixtures/with-config/nuxt.config.js index 54e22ee28a..31d2f9c413 100644 --- a/test/fixtures/with-config/nuxt.config.js +++ b/test/fixtures/with-config/nuxt.config.js @@ -20,6 +20,9 @@ export default { { path: '/redirect/about-bis', redirect: '/about-bis' + }, + { + path: '/not-existed' } ] } diff --git a/test/unit/with-config.test.js b/test/unit/with-config.test.js index 22bf6fe7db..9568fce82c 100644 --- a/test/unit/with-config.test.js +++ b/test/unit/with-config.test.js @@ -112,6 +112,11 @@ describe('with-config', () => { expect(html.includes('

About page

')).toBe(true) }) + test('/test/not-existed should return 404', async () => { + await expect(rp(url('/test/not-existed'))) + .rejects.toMatchObject({ statusCode: 404 }) + }) + test('/test/redirect/about-bis (redirect with extendRoutes)', async () => { const window = await nuxt.renderAndGetWindow(url('/test/redirect/about-bis')) const windowHref = window.location.href