Skip to content

Commit e80643b

Browse files
committed
fix: move addNavGuards check to mounted hook
The addNavGuards check adds the navigation guards when an afterNavigation callback is defined but refreshOnceOnNavigation was not set. As the afterNavigation callback is defined in metaInfo which can be dependent on user data we need to wait until all components are fully mounted before checking if a afterNavigation callback was defined Fixes: #348
1 parent 9d18b50 commit e80643b

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/shared/mixin.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ export default function createMixin(Vue, options) {
7676
if (!this.$root._vueMeta.initialized) {
7777
// refresh meta in nextTick so all child components have loaded
7878
this.$nextTick(function () {
79-
this.$root.$meta().refresh()
79+
const { metaInfo } = this.$root.$meta().refresh()
8080
this.$root._vueMeta.initialized = true
81+
82+
// add the navigation guards if they havent been added yet
83+
// they are needed for the afterNavigation callback
84+
if (!options.refreshOnceOnNavigation && metaInfo.afterNavigation) {
85+
addNavGuards(this)
86+
}
8187
})
8288
}
8389
})
@@ -91,16 +97,6 @@ export default function createMixin(Vue, options) {
9197

9298
// do not trigger refresh on the server side
9399
if (!this.$isServer) {
94-
// add the navigation guards if they havent been added yet
95-
// if metaInfo is defined as a function, this does call the computed fn redundantly
96-
// but as Vue internally caches the results of computed props it shouldnt hurt performance
97-
if (!options.refreshOnceOnNavigation && (
98-
(this.$options[options.keyName] && this.$options[options.keyName].afterNavigation) ||
99-
(this.$options.computed && this.$options.computed.$metaInfo && (this.$options.computed.$metaInfo() || {}).afterNavigation)
100-
)) {
101-
addNavGuards(this)
102-
}
103-
104100
// no need to add this hooks on server side
105101
updateOnLifecycleHook.forEach((lifecycleHook) => {
106102
ensuredPush(this.$options, lifecycleHook, () => triggerUpdate(this, lifecycleHook))

test/unit/components.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('client', () => {
128128
expect(context._uid).toBe(wrapper.vm._uid)
129129
})
130130

131-
test('afterNavigation function is called with refreshOnce: true', () => {
131+
test('afterNavigation function is called with refreshOnce: true', async () => {
132132
const Vue = loadVueMetaPlugin(false, { refreshOnceOnNavigation: true })
133133
const afterNavigation = jest.fn()
134134
const component = Vue.component('nav-component', {
@@ -151,6 +151,8 @@ describe('client', () => {
151151
}
152152
})
153153

154+
await vmTick(wrapper.vm)
155+
154156
expect(guards.before).toBeDefined()
155157
expect(guards.after).toBeDefined()
156158

@@ -161,7 +163,7 @@ describe('client', () => {
161163
expect(afterNavigation).toHaveBeenCalled()
162164
})
163165

164-
test('afterNavigation function is called with refreshOnce: false', () => {
166+
test('afterNavigation function is called with refreshOnce: false', async () => {
165167
const Vue = loadVueMetaPlugin(false, { refreshOnceOnNavigation: false })
166168
const afterNavigation = jest.fn()
167169
const component = Vue.component('nav-component', {
@@ -184,6 +186,8 @@ describe('client', () => {
184186
}
185187
})
186188

189+
await vmTick(wrapper.vm)
190+
187191
expect(guards.before).toBeDefined()
188192
expect(guards.after).toBeDefined()
189193

0 commit comments

Comments
 (0)