2023-07-05 12:33:12 +00:00
|
|
|
import { defineComponent, h, nextTick, onMounted, provide, shallowReactive } from 'vue'
|
2023-07-05 10:39:39 +00:00
|
|
|
import type { Ref, VNode } from 'vue'
|
|
|
|
import type { RouteLocation, RouteLocationNormalizedLoaded } from '#vue-router'
|
2023-10-30 21:05:02 +00:00
|
|
|
import { PageRouteSymbol } from './injections'
|
2023-07-05 10:39:39 +00:00
|
|
|
|
|
|
|
export const RouteProvider = defineComponent({
|
|
|
|
props: {
|
|
|
|
vnode: {
|
|
|
|
type: Object as () => VNode,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
route: {
|
|
|
|
type: Object as () => RouteLocationNormalizedLoaded,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
vnodeRef: Object as () => Ref<any>,
|
|
|
|
renderKey: String,
|
|
|
|
trackRootNodes: Boolean
|
|
|
|
},
|
|
|
|
setup (props) {
|
|
|
|
// Prevent reactivity when the page will be rerendered in a different suspense fork
|
|
|
|
const previousKey = props.renderKey
|
|
|
|
const previousRoute = props.route
|
|
|
|
|
|
|
|
// Provide a reactive route within the page
|
|
|
|
const route = {} as RouteLocation
|
|
|
|
for (const key in props.route) {
|
2023-07-05 12:33:12 +00:00
|
|
|
Object.defineProperty(route, key, {
|
|
|
|
get: () => previousKey === props.renderKey ? props.route[key as keyof RouteLocationNormalizedLoaded] : previousRoute[key as keyof RouteLocationNormalizedLoaded]
|
|
|
|
})
|
2023-07-05 10:39:39 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 12:33:12 +00:00
|
|
|
provide(PageRouteSymbol, shallowReactive(route))
|
2023-07-05 10:39:39 +00:00
|
|
|
|
|
|
|
let vnode: VNode
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.dev && import.meta.client && props.trackRootNodes) {
|
2023-07-05 10:39:39 +00:00
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (['#comment', '#text'].includes(vnode?.el?.nodeName)) {
|
|
|
|
const filename = (vnode?.type as any).__file
|
|
|
|
console.warn(`[nuxt] \`${filename}\` does not have a single root node and will cause errors when navigating between routes.`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.dev && import.meta.client) {
|
2023-07-05 10:39:39 +00:00
|
|
|
vnode = h(props.vnode, { ref: props.vnodeRef })
|
|
|
|
return vnode
|
|
|
|
}
|
|
|
|
|
|
|
|
return h(props.vnode, { ref: props.vnodeRef })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|