app: Better external url redirect handling

This commit is contained in:
Sébastien Chopin 2018-01-15 12:22:51 +01:00
parent cc4e0663e8
commit de02ea4b5d
5 changed files with 27 additions and 21 deletions

View File

@ -7,9 +7,6 @@ export default {
name: 'nuxt', name: 'nuxt',
props: ['nuxtChildKey'], props: ['nuxtChildKey'],
render(h) { render(h) {
if (this.nuxt._redirected) {
return h('div', [ '<%= messages.redirect %>' ])
}
// If there is some error // If there is some error
if (this.nuxt.err) { if (this.nuxt.err) {
return h('nuxt-error', { return h('nuxt-error', {

View File

@ -137,6 +137,7 @@ export async function setContext(app, context) {
if (pathType === 'object') { if (pathType === 'object') {
path = app.router.resolve(path).href path = app.router.resolve(path).href
} }
console.log(path, query)
// "/absolute/route", "./relative/route" or "../relative/route" // "/absolute/route", "./relative/route" or "../relative/route"
if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) { if (/(^[.]{1,2}\/)|(^\/(?!\/))/.test(path)) {
app.context.next({ app.context.next({
@ -147,12 +148,16 @@ export async function setContext(app, context) {
} else { } else {
path = formatUrl(path, query) path = formatUrl(path, query)
if (process.server) { if (process.server) {
app.context.res.setHeader('Location', path) app.context.next({
app.context.res.statusCode = status path: path,
app.nuxt._redirected = true status: status
})
} }
if (process.client) { if (process.client) {
window.location = path window.location = path
return new Promise((resolve) => {
// Wait for broswer to redirect...
})
} }
} }
} }

View File

@ -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.', '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: 'Error',
client_error_details: client_error_details:
'An error occurred while rendering the page. Check developer tools console for details.', 'An error occurred while rendering the page. Check developer tools console for details.'
redirect: 'Redirecting to external page.'
} }
} }

View File

@ -84,7 +84,7 @@ test.serial('/store', async t => {
test.serial('/head', async t => { test.serial('/head', async t => {
const msg = new Promise(resolve => const msg = new Promise(resolve =>
page.on('console', msg => resolve(msg.text)) page.on('console', msg => resolve(msg.text()))
) )
await page.nuxt.navigate('/head') await page.nuxt.navigate('/head')
const metas = await page.$$attr('meta', 'content') 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 => { test.serial('/router-guard', async t => {
await page.nuxt.navigate('/router-guard') 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 // 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() await nuxt.close()
}) })
test.after.always('Stop browser', async t => {
await browser.stop()
})

View File

@ -146,16 +146,19 @@ test('/redirect -> check redirected source', async t => {
}) })
test('/redirect -> external link', async t => { test('/redirect -> external link', async t => {
const headers = {} let _headers, _status
const { html } = await nuxt.renderRoute('/redirect-external', { const { html } = await nuxt.renderRoute('/redirect-external', {
res: { res: {
setHeader(k, v) { writeHead(status, headers) {
headers[k] = v _status = status
} _headers = headers
},
end() {}
} }
}) })
t.is(headers.Location, 'https://nuxtjs.org') t.is(_status, 302)
t.true(html.includes('<div>Redirecting to external page.</div>')) t.is(_headers.Location, 'https://nuxtjs.org')
t.true(html.includes('<div data-server-rendered="true"></div>'))
}) })
test('/special-state -> check window.__NUXT__.test = true', async t => { test('/special-state -> check window.__NUXT__.test = true', async t => {