From de02ea4b5dee0c130867f87043453ddf3da41ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Mon, 15 Jan 2018 12:22:51 +0100 Subject: [PATCH] app: Better external url redirect handling --- lib/app/components/nuxt.js | 3 --- lib/app/utils.js | 11 ++++++++--- lib/common/options.js | 3 +-- test/basic.csr.test.js | 16 +++++++++------- test/basic.ssr.test.js | 15 +++++++++------ 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/app/components/nuxt.js b/lib/app/components/nuxt.js index 380bf5f110..d6eee5c000 100644 --- a/lib/app/components/nuxt.js +++ b/lib/app/components/nuxt.js @@ -7,9 +7,6 @@ export default { name: 'nuxt', props: ['nuxtChildKey'], render(h) { - if (this.nuxt._redirected) { - return h('div', [ '<%= messages.redirect %>' ]) - } // If there is some error if (this.nuxt.err) { return h('nuxt-error', { diff --git a/lib/app/utils.js b/lib/app/utils.js index 7942fbdd21..b29804577a 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -137,6 +137,7 @@ export async function setContext(app, context) { if (pathType === 'object') { path = app.router.resolve(path).href } + console.log(path, query) // "/absolute/route", "./relative/route" or "../relative/route" if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) { app.context.next({ @@ -147,12 +148,16 @@ export async function setContext(app, context) { } else { path = formatUrl(path, query) if (process.server) { - app.context.res.setHeader('Location', path) - app.context.res.statusCode = status - app.nuxt._redirected = true + app.context.next({ + path: path, + status: status + }) } if (process.client) { window.location = path + return new Promise((resolve) => { + // Wait for broswer to redirect... + }) } } } diff --git a/lib/common/options.js b/lib/common/options.js index d4648917c2..3a1bf9f5ac 100755 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -324,7 +324,6 @@ Options.defaults = { 'An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.', client_error: 'Error', client_error_details: - 'An error occurred while rendering the page. Check developer tools console for details.', - redirect: 'Redirecting to external page.' + 'An error occurred while rendering the page. Check developer tools console for details.' } } diff --git a/test/basic.csr.test.js b/test/basic.csr.test.js index ff6f3cb3d6..eb430a62a0 100644 --- a/test/basic.csr.test.js +++ b/test/basic.csr.test.js @@ -84,7 +84,7 @@ test.serial('/store', async t => { test.serial('/head', async t => { const msg = new Promise(resolve => - page.on('console', msg => resolve(msg.text)) + page.on('console', msg => resolve(msg.text())) ) await page.nuxt.navigate('/head') const metas = await page.$$attr('meta', 'content') @@ -209,14 +209,16 @@ test.serial('/fn-midd?please=true', async t => { test.serial('/router-guard', async t => { await page.nuxt.navigate('/router-guard') - t.is(await page.$text('p'), 'Nuxt.js') + const p = await page.$text('p') + t.is(p, 'Nuxt.js') +}) + +test.after.always('Stop browser', async () => { + process.on('unhandledRejection', () => {}) + await browser.stop() }) // Close server and ask nuxt to stop listening to file changes -test.after.always('Closing server and nuxt.js', async t => { +test.after.always('Closing server and nuxt.js', async () => { await nuxt.close() }) - -test.after.always('Stop browser', async t => { - await browser.stop() -}) diff --git a/test/basic.ssr.test.js b/test/basic.ssr.test.js index fdc65fc967..26e7cf107e 100755 --- a/test/basic.ssr.test.js +++ b/test/basic.ssr.test.js @@ -146,16 +146,19 @@ test('/redirect -> check redirected source', async t => { }) test('/redirect -> external link', async t => { - const headers = {} + let _headers, _status const { html } = await nuxt.renderRoute('/redirect-external', { res: { - setHeader(k, v) { - headers[k] = v - } + writeHead(status, headers) { + _status = status + _headers = headers + }, + end() {} } }) - t.is(headers.Location, 'https://nuxtjs.org') - t.true(html.includes('
Redirecting to external page.
')) + t.is(_status, 302) + t.is(_headers.Location, 'https://nuxtjs.org') + t.true(html.includes('
')) }) test('/special-state -> check window.__NUXT__.test = true', async t => {