Nuxt/packages/vue-app/template/components/nuxt.js

111 lines
3.1 KiB
JavaScript
Raw Normal View History

import Vue from 'vue'
2018-10-24 13:46:06 +00:00
import { compile } from '../utils'
<% if (components.ErrorPage) { %>
<% if (('~@').includes(components.ErrorPage.charAt(0))) { %>
import NuxtError from '<%= components.ErrorPage %>'
<% } else { %>
import NuxtError from '<%= "../" + components.ErrorPage %>'
<% } %>
<% } else { %>
import NuxtError from './nuxt-error.vue'
<% } /* components */ %>
2018-10-24 13:46:06 +00:00
import NuxtChild from './nuxt-child'
2019-01-06 07:56:59 +00:00
<%= isTest ? '// @vue/component' : '' %>
export default {
name: 'Nuxt',
2019-01-06 07:56:59 +00:00
components: {
NuxtChild,
NuxtError
},
2018-10-24 13:46:06 +00:00
props: {
2019-01-06 07:56:59 +00:00
nuxtChildKey: {
type: String,
default: undefined
},
keepAlive: Boolean,
2019-01-06 07:56:59 +00:00
keepAliveProps: {
type: Object,
default: undefined
},
name: {
type: String,
default: 'default'
}
2018-10-24 13:46:06 +00:00
},
errorCaptured (error) {
// if we receive and error while showing the NuxtError component
// capture the error and force an immediate update so we re-render
// without the NuxtError component
if (this.displayingNuxtError) {
this.errorFromNuxtError = error
this.$forceUpdate()
}
},
2019-01-06 07:56:59 +00:00
computed: {
2019-09-10 09:51:14 +00:00
routerViewKey () {
2019-01-06 07:56:59 +00:00
// If nuxtChildKey prop is given or current route has children
if (typeof this.nuxtChildKey !== 'undefined' || this.$route.matched.length > 1) {
return this.nuxtChildKey || compile(this.$route.matched[0].path)(this.$route.params)
}
const [matchedRoute] = this.$route.matched
if (!matchedRoute) {
return this.$route.path
}
const Component = matchedRoute.components.default
if (Component && Component.options) {
const { options } = Component
if (options.key) {
return (typeof options.key === 'function' ? options.key(this.$route) : options.key)
}
2019-01-06 07:56:59 +00:00
}
const strict = /\/$/.test(matchedRoute.path)
return strict ? this.$route.path : this.$route.path.replace(/\/$/, '')
2019-01-06 07:56:59 +00:00
}
},
2019-09-10 09:51:14 +00:00
beforeCreate () {
2019-01-06 07:56:59 +00:00
Vue.util.defineReactive(this, 'nuxt', this.$root.$options.nuxt)
},
2019-09-10 09:51:14 +00:00
render (h) {
// if there is no error or
// error page has not been loaded yet on client
if (!this.nuxt.err || (process.client && !this.nuxt.errPageReady)) {
// Directly return nuxt child
return h('NuxtChild', {
key: this.routerViewKey,
props: this.$props
2017-10-07 09:47:31 +00:00
})
}
2020-04-14 09:26:40 +00:00
// if an error occurred within NuxtError show a simple
// error message instead to prevent looping
if (this.errorFromNuxtError) {
this.$nextTick(() => (this.errorFromNuxtError = false))
return h('div', {}, [
2020-04-14 09:26:40 +00:00
h('h2', 'An error occurred while showing the error page'),
h('p', 'Unfortunately an error occurred and while showing the error page another error occurred'),
h('p', `Error details: ${this.errorFromNuxtError.toString()}`),
h('nuxt-link', { props: { to: '/' } }, 'Go back to home')
])
}
// track if we are showing the NuxtError component
this.displayingNuxtError = true
this.$nextTick(() => (this.displayingNuxtError = false))
return h(NuxtError, {
props: {
error: this.nuxt.err
}
2017-10-07 09:47:31 +00:00
})
}
}