1
1
import triggerUpdate from '../client/triggerUpdate'
2
- import hasMetaInfo from './hasMetaInfo '
2
+ import { hasMetaInfo } from './meta-helpers '
3
3
import { isUndefined , isFunction } from './is-type'
4
4
import { ensuredPush } from './ensure'
5
+ import { addNavGuards } from './nav-guards'
5
6
6
7
export default function createMixin ( Vue , options ) {
7
8
// for which Vue lifecycle hooks should the metaInfo be refreshed
@@ -14,7 +15,7 @@ export default function createMixin(Vue, options) {
14
15
get ( ) {
15
16
// Show deprecation warning once when devtools enabled
16
17
if ( Vue . config . devtools && ! this . $root . _vueMeta . hasMetaInfoDeprecationWarningShown ) {
17
- console . warn ( 'VueMeta DeprecationWarning: _hasMetaInfo has been deprecated and will be removed in a future version. Please import hasMetaInfo and use hasMetaInfo(vm) instead' ) // eslint-disable-line no-console
18
+ console . warn ( 'VueMeta DeprecationWarning: _hasMetaInfo has been deprecated and will be removed in a future version. Please use hasMetaInfo(vm) instead' ) // eslint-disable-line no-console
18
19
this . $root . _vueMeta . hasMetaInfoDeprecationWarningShown = true
19
20
}
20
21
return hasMetaInfo ( this )
@@ -71,39 +72,32 @@ export default function createMixin(Vue, options) {
71
72
this . $root . _vueMeta . initialized = this . $isServer
72
73
73
74
if ( ! this . $root . _vueMeta . initialized ) {
74
- const $rootMeta = this . $root . $meta ( )
75
-
76
75
ensuredPush ( this . $options , 'mounted' , ( ) => {
77
76
if ( ! this . $root . _vueMeta . initialized ) {
78
77
// refresh meta in nextTick so all child components have loaded
79
78
this . $nextTick ( function ( ) {
80
- $rootMeta . refresh ( )
79
+ this . $root . $meta ( ) . refresh ( )
81
80
this . $root . _vueMeta . initialized = true
82
81
} )
83
82
}
84
83
} )
85
84
86
- // add vue-router navigation guard to prevent multiple updates during navigation
87
- // only usefull on the client side
88
- if ( options . refreshOnceOnNavigation && this . $root . $router ) {
89
- const $router = this . $root . $router
90
- $router . beforeEach ( ( to , from , next ) => {
91
- $rootMeta . pause ( )
92
- next ( )
93
- } )
94
-
95
- $router . afterEach ( ( ) => {
96
- const { vm, metaInfo } = $rootMeta . resume ( )
97
- if ( metaInfo && metaInfo . afterNavigation && isFunction ( metaInfo . afterNavigation ) ) {
98
- metaInfo . afterNavigation . call ( vm , metaInfo )
99
- }
100
- } )
85
+ // add the navigation guards if they havent been added yet
86
+ if ( options . refreshOnceOnNavigation ) {
87
+ addNavGuards ( this )
101
88
}
102
89
}
103
90
}
104
91
105
92
// do not trigger refresh on the server side
106
93
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 ( this . $options [ options . keyName ] . afterNavigation ) {
98
+ addNavGuards ( this )
99
+ }
100
+
107
101
// no need to add this hooks on server side
108
102
updateOnLifecycleHook . forEach ( ( lifecycleHook ) => {
109
103
ensuredPush ( this . $options , lifecycleHook , ( ) => triggerUpdate ( this , lifecycleHook ) )
0 commit comments