2022-08-04 11:30:18 +00:00
|
|
|
import { computed, DefineComponent, defineComponent, h, inject, provide, reactive, Suspense, Transition } from 'vue'
|
2022-08-12 17:47:58 +00:00
|
|
|
import { RouteLocation, RouteLocationNormalized, RouteLocationNormalizedLoaded, RouterView } from 'vue-router'
|
2022-01-25 14:32:09 +00:00
|
|
|
|
2022-03-14 10:47:24 +00:00
|
|
|
import { generateRouteKey, RouterViewSlotProps, wrapInKeepAlive } from './utils'
|
2022-02-07 11:32:04 +00:00
|
|
|
import { useNuxtApp } from '#app'
|
2022-03-14 10:47:24 +00:00
|
|
|
import { _wrapIf } from '#app/components/utils'
|
2022-01-25 14:32:09 +00:00
|
|
|
|
2022-04-19 08:38:57 +00:00
|
|
|
const isNestedKey = Symbol('isNested')
|
|
|
|
|
2022-01-25 14:32:09 +00:00
|
|
|
export default defineComponent({
|
|
|
|
name: 'NuxtPage',
|
2022-06-03 14:01:46 +00:00
|
|
|
inheritAttrs: false,
|
2022-02-07 11:32:04 +00:00
|
|
|
props: {
|
2022-06-03 14:01:46 +00:00
|
|
|
name: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
route: {
|
|
|
|
type: Object as () => RouteLocationNormalized
|
|
|
|
},
|
2022-02-07 11:32:04 +00:00
|
|
|
pageKey: {
|
|
|
|
type: [Function, String] as unknown as () => string | ((route: RouteLocationNormalizedLoaded) => string),
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
2022-06-03 14:01:46 +00:00
|
|
|
setup (props, { attrs }) {
|
2022-01-25 14:32:09 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
|
2022-04-19 08:38:57 +00:00
|
|
|
const isNested = inject(isNestedKey, false)
|
|
|
|
provide(isNestedKey, true)
|
|
|
|
|
2022-01-25 14:32:09 +00:00
|
|
|
return () => {
|
2022-06-03 14:01:46 +00:00
|
|
|
return h(RouterView, { name: props.name, route: props.route, ...attrs }, {
|
2022-08-02 09:58:03 +00:00
|
|
|
default: (routeProps: RouterViewSlotProps) => {
|
|
|
|
if (!routeProps.Component) { return }
|
|
|
|
|
2022-08-04 11:30:18 +00:00
|
|
|
const key = generateRouteKey(props.pageKey, routeProps)
|
|
|
|
|
2022-08-02 09:58:03 +00:00
|
|
|
return _wrapIf(Transition, routeProps.route.meta.pageTransition ?? defaultPageTransition,
|
|
|
|
wrapInKeepAlive(routeProps.route.meta.keepalive, isNested && nuxtApp.isHydrating
|
|
|
|
// Include route children in parent suspense
|
2022-08-04 11:30:18 +00:00
|
|
|
? h(Component, { key, routeProps, pageKey: key } as {})
|
2022-08-02 09:58:03 +00:00
|
|
|
: h(Suspense, {
|
|
|
|
onPending: () => nuxtApp.callHook('page:start', routeProps.Component),
|
|
|
|
onResolve: () => nuxtApp.callHook('page:finish', routeProps.Component)
|
2022-08-04 11:30:18 +00:00
|
|
|
}, { default: () => h(Component, { key, routeProps, pageKey: key } as {}) })
|
2022-08-02 09:58:03 +00:00
|
|
|
)).default()
|
|
|
|
}
|
2022-01-25 14:32:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 14:01:46 +00:00
|
|
|
}) as DefineComponent<{
|
|
|
|
name?: string,
|
|
|
|
route?: RouteLocationNormalized
|
|
|
|
pageKey?: string | ((route: RouteLocationNormalizedLoaded) => string)
|
|
|
|
[key: string]: any
|
|
|
|
}>
|
2022-01-25 14:32:09 +00:00
|
|
|
|
|
|
|
const defaultPageTransition = { name: 'page', mode: 'out-in' }
|
2022-08-03 10:38:46 +00:00
|
|
|
|
|
|
|
const Component = defineComponent({
|
2022-08-12 17:47:58 +00:00
|
|
|
// TODO: Type props
|
2022-08-04 11:30:18 +00:00
|
|
|
// eslint-disable-next-line vue/require-prop-types
|
|
|
|
props: ['routeProps', 'pageKey'],
|
2022-08-03 10:38:46 +00:00
|
|
|
setup (props) {
|
2022-08-04 11:30:18 +00:00
|
|
|
// Prevent reactivity when the page will be rerendered in a different suspense fork
|
|
|
|
const previousKey = props.pageKey
|
|
|
|
const previousRoute = props.routeProps.route
|
|
|
|
|
|
|
|
// Provide a reactive route within the page
|
2022-08-12 17:47:58 +00:00
|
|
|
const route = {} as RouteLocation
|
2022-08-04 11:30:18 +00:00
|
|
|
for (const key in props.routeProps.route) {
|
2022-08-12 17:47:58 +00:00
|
|
|
(route as any)[key] = computed(() => previousKey === props.pageKey ? props.routeProps.route[key] : previousRoute[key])
|
2022-08-04 11:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
provide('_route', reactive(route))
|
2022-08-03 10:38:46 +00:00
|
|
|
return () => h(props.routeProps.Component)
|
|
|
|
}
|
|
|
|
})
|