mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
app: Better external url redirect handling
This commit is contained in:
parent
cc4e0663e8
commit
de02ea4b5d
@ -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)
|
||||
// "/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')
|
||||
})
|
||||
|
||||
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()
|
||||
})
|
||||
|
@ -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 => {
|
||||
|
Loading…
Reference in New Issue
Block a user