Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxt): make route provided to page children reactive (#6349)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Aug 4, 2022
1 parent 23329e0 commit 1f00a9f
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions packages/nuxt/src/pages/runtime/page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DefineComponent, defineComponent, h, inject, provide, Suspense, Transition } from 'vue'
import { computed, DefineComponent, defineComponent, h, inject, provide, reactive, Suspense, Transition } from 'vue'
import { RouteLocationNormalized, RouteLocationNormalizedLoaded, RouterView } from 'vue-router'

import { generateRouteKey, RouterViewSlotProps, wrapInKeepAlive } from './utils'
Expand Down Expand Up @@ -33,14 +33,16 @@ export default defineComponent({
default: (routeProps: RouterViewSlotProps) => {
if (!routeProps.Component) { return }

const key = generateRouteKey(props.pageKey, routeProps)

return _wrapIf(Transition, routeProps.route.meta.pageTransition ?? defaultPageTransition,
wrapInKeepAlive(routeProps.route.meta.keepalive, isNested && nuxtApp.isHydrating
// Include route children in parent suspense
? h(Component, { key: generateRouteKey(props.pageKey, routeProps), routeProps } as {})
? h(Component, { key, routeProps, pageKey: key } as {})
: h(Suspense, {
onPending: () => nuxtApp.callHook('page:start', routeProps.Component),
onResolve: () => nuxtApp.callHook('page:finish', routeProps.Component)
}, { default: () => h(Component, { key: generateRouteKey(props.pageKey, routeProps), routeProps } as {}) })
}, { default: () => h(Component, { key, routeProps, pageKey: key } as {}) })
)).default()
}
})
Expand All @@ -56,9 +58,20 @@ export default defineComponent({
const defaultPageTransition = { name: 'page', mode: 'out-in' }

const Component = defineComponent({
props: ['routeProps'],
// eslint-disable-next-line vue/require-prop-types
props: ['routeProps', 'pageKey'],
setup (props) {
provide('_route', props.routeProps.route)
// 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
const route = {}
for (const key in props.routeProps.route) {
route[key] = computed(() => previousKey === props.pageKey ? props.routeProps.route[key] : previousRoute[key])
}

provide('_route', reactive(route))
return () => h(props.routeProps.Component)
}
})

0 comments on commit 1f00a9f

Please sign in to comment.