mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat: allow redirect to external url
This commit is contained in:
parent
b31b0f250c
commit
21f9145309
@ -7,6 +7,9 @@ export default {
|
||||
name: 'nuxt',
|
||||
props: ['nuxtChildKey'],
|
||||
render(h) {
|
||||
if (this.nuxt._redirected) {
|
||||
return h('div', [ 'redirecting.' ])
|
||||
}
|
||||
// If there is some error
|
||||
if (this.nuxt.err) {
|
||||
return h('nuxt-error', {
|
||||
|
@ -132,11 +132,23 @@ export async function setContext(app, context) {
|
||||
path = status
|
||||
status = 302
|
||||
}
|
||||
app.context.next({
|
||||
path: path,
|
||||
query: query,
|
||||
status: status
|
||||
})
|
||||
if (path.charAt(0) === '/' && path.charAt(1) !== '/') {
|
||||
app.context.next({
|
||||
path: path,
|
||||
query: query,
|
||||
status: status
|
||||
})
|
||||
} else {
|
||||
path = formatUrl(path, query)
|
||||
if (process.server) {
|
||||
app.context.res.setHeader('Location', path)
|
||||
app.context.res.statusCode = status
|
||||
app.nuxt._redirected = true
|
||||
}
|
||||
if (process.client) {
|
||||
window.location = path
|
||||
}
|
||||
}
|
||||
}
|
||||
if (process.server) app.context.beforeNuxtRender = (fn) => context.beforeRenderFns.push(fn)
|
||||
if (process.client) app.context.nuxtState = window.__NUXT__
|
||||
@ -442,3 +454,60 @@ function escapeString(str) {
|
||||
function escapeGroup(group) {
|
||||
return group.replace(/([=!:$\/()])/g, '\\$1')
|
||||
}
|
||||
|
||||
/**
|
||||
* Format given url, append query to url query string
|
||||
*
|
||||
* @param {string} url
|
||||
* @param {string} query
|
||||
* @return {string}
|
||||
*/
|
||||
function formatUrl (url, query) {
|
||||
let protocol
|
||||
let index = url.indexOf('://')
|
||||
if (index !== -1) {
|
||||
protocol = url.substring(0, index)
|
||||
url = url.substring(index + 3)
|
||||
} else if (url.indexOf('//') === 0) {
|
||||
url = url.substring(2)
|
||||
}
|
||||
|
||||
let parts = url.split('/')
|
||||
let result = (protocol ? protocol + '://' : '//') + parts.shift()
|
||||
|
||||
let path = parts.filter(Boolean).join('/')
|
||||
let hash
|
||||
parts = path.split('#')
|
||||
if (parts.length === 2) {
|
||||
path = parts[0]
|
||||
hash = parts[1]
|
||||
}
|
||||
|
||||
result += path ? '/' + path : ''
|
||||
|
||||
if (query && JSON.stringify(query) !== '{}') {
|
||||
result += (url.split('?').length === 2 ? '&' : '?') + formatQuery(query)
|
||||
}
|
||||
result += hash ? '#' + hash : ''
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform data object to query string
|
||||
*
|
||||
* @param {object} query
|
||||
* @return {string}
|
||||
*/
|
||||
function formatQuery (query) {
|
||||
return Object.keys(query).sort().map(key => {
|
||||
var val = query[key]
|
||||
if (val == null) {
|
||||
return ''
|
||||
}
|
||||
if (Array.isArray(val)) {
|
||||
return val.slice().map(val2 => [key, '=', val2].join('')).join('&')
|
||||
}
|
||||
return key + '=' + val
|
||||
}).filter(Boolean).join('&')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user