Permalink
Browse files

app: Better external url redirect handling

  • Loading branch information...
Atinux committed Jan 15, 2018
1 parent cc4e066 commit de02ea4b5dee0c130867f87043453ddf3da41ea5
Showing with 27 additions and 21 deletions.
  1. +0 −3 lib/app/components/nuxt.js
  2. +8 −3 lib/app/utils.js
  3. +1 −2 lib/common/options.js
  4. +9 −7 test/basic.csr.test.js
  5. +9 −6 test/basic.ssr.test.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', {
@@ -137,6 +137,7 @@ export async function setContext(app, context) {
if (pathType === 'object') {
path = app.router.resolve(path).href
}
console.log(path, query)

This comment has been minimized.

@pi0

pi0 Jan 15, 2018

Member

Is this necessary?

This comment has been minimized.

@Atinux

Atinux Jan 15, 2018

Member

Hello no, removing it...

// "/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...
})
}
}
}
@@ -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.'
}
}
@@ -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')
})

// Close server and ask nuxt to stop listening to file changes
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
test.after.always('Stop browser', async () => {
process.on('unhandledRejection', () => {})
await browser.stop()
})

test.after.always('Stop browser', async t => {
await browser.stop()
// Close server and ask nuxt to stop listening to file changes
test.after.always('Closing server and nuxt.js', async () => {
await nuxt.close()
})
@@ -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('<div>Redirecting to external page.</div>'))
t.is(_status, 302)
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 => {

0 comments on commit de02ea4

Please sign in to comment.