diff --git a/lib/app/utils.js b/lib/app/utils.js index cb2642ced0..7942fbdd21 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -127,11 +127,16 @@ export async function setContext(app, context) { if (!status) return app.context._redirected = true // Used in middleware // if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' }) - if (typeof status === 'string' && (typeof path === 'undefined' || typeof path === 'object')) { + let pathType = typeof path + if (typeof status !== 'number' && (pathType === 'undefined' || pathType === 'object')) { query = path || {} path = status + pathType = typeof path status = 302 } + if (pathType === 'object') { + path = app.router.resolve(path).href + } // "/absolute/route", "./relative/route" or "../relative/route" if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) { app.context.next({ diff --git a/test/basic.csr.test.js b/test/basic.csr.test.js index b8108dae16..0329e398b8 100644 --- a/test/basic.csr.test.js +++ b/test/basic.csr.test.js @@ -148,21 +148,27 @@ test.serial('/error2', async t => { t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' }) }) -test.serial('/redirect2', async t => { - await page.nuxt.navigate('/redirect2') +test.serial('/redirect-middleware', async t => { + await page.nuxt.navigate('/redirect-middleware') t.is(await page.$text('h1'), 'Index page') }) -test.serial('/redirect3', async t => { +test.serial('/redirect-external', async t => { // New page for redirecting to external link. const page = await browser.page(url('/')) - await page.nuxt.navigate('/redirect3', false) + await page.nuxt.navigate('/redirect-external', false) await page.waitForFunction(() => window.location.href === 'https://nuxtjs.org/') page.close() t.pass() }) +test.serial('/redirect-name', async t => { + await page.nuxt.navigate('/redirect-name') + + t.is(await page.$text('h1'), 'My component!') +}) + test.serial('/no-ssr', async t => { await page.nuxt.navigate('/no-ssr') diff --git a/test/basic.ssr.test.js b/test/basic.ssr.test.js index d023acaa8b..5e5ee17305 100755 --- a/test/basic.ssr.test.js +++ b/test/basic.ssr.test.js @@ -132,7 +132,7 @@ test('/redirect -> check redirected source', async t => { test('/redirect -> external link', async t => { const headers = {} - const { html } = await nuxt.renderRoute('/redirect3', { + const { html } = await nuxt.renderRoute('/redirect-external', { res: { setHeader(k, v) { headers[k] = v @@ -187,14 +187,21 @@ test.serial('/error-midd', async t => { t.true(errorSpy.notCalled) }) -test.serial('/redirect2', async t => { +test.serial('/redirect-middleware', async t => { const errorSpy = await interceptError() - await rp(url('/redirect2')) // Should not console.error + await rp(url('/redirect-middleware')) // Should not console.error release() // Don't display error since redirect returns a noopApp t.true(errorSpy.notCalled) }) +test('/redirect-name', async t => { + const { html, redirected } = await nuxt.renderRoute('/redirect-name') + t.true(html.includes('
')) + t.true(redirected.path === '/stateless') + t.true(redirected.status === 302) +}) + test('/no-ssr', async t => { const { html } = await nuxt.renderRoute('/no-ssr') t.true(html.includes('
<p>Loading...</p>
')) diff --git a/test/fixtures/basic/pages/redirect3.vue b/test/fixtures/basic/pages/redirect-external.vue similarity index 100% rename from test/fixtures/basic/pages/redirect3.vue rename to test/fixtures/basic/pages/redirect-external.vue diff --git a/test/fixtures/basic/pages/redirect2.vue b/test/fixtures/basic/pages/redirect-middleware.vue similarity index 100% rename from test/fixtures/basic/pages/redirect2.vue rename to test/fixtures/basic/pages/redirect-middleware.vue diff --git a/test/fixtures/basic/pages/redirect-name.vue b/test/fixtures/basic/pages/redirect-name.vue new file mode 100644 index 0000000000..0797e27eaa --- /dev/null +++ b/test/fixtures/basic/pages/redirect-name.vue @@ -0,0 +1,11 @@ + + +