1
- import batchUpdate from '../client/batchUpdate '
1
+ import triggerUpdate from '../client/triggerUpdate '
2
2
import { isUndefined , isFunction } from '../shared/typeof'
3
+ import { ensuredPush } from '../shared/ensure'
3
4
4
5
export default function createMixin ( options ) {
5
- // store an id to keep track of DOM updates
6
- let batchID = null
7
-
8
6
// for which Vue lifecycle hooks should the metaInfo be refreshed
9
7
const updateOnLifecycleHook = [ 'activated' , 'deactivated' , 'beforeMount' ]
10
8
11
- const triggerUpdate = ( vm ) => {
12
- if ( vm . $root . _vueMetaInitialized ) {
13
- // batch potential DOM updates to prevent extraneous re-rendering
14
- batchID = batchUpdate ( batchID , ( ) => vm . $meta ( ) . refresh ( ) )
15
- }
16
- }
17
-
18
9
// watch for client side component updates
19
10
return {
20
11
beforeCreate ( ) {
@@ -36,41 +27,41 @@ export default function createMixin(options) {
36
27
// if computed $metaInfo exists, watch it for updates & trigger a refresh
37
28
// when it changes (i.e. automatically handle async actions that affect metaInfo)
38
29
// credit for this suggestion goes to [Sébastien Chopin](https://github.com/Atinux)
39
- this . $options . created = this . $options . created || [ ]
40
- this . $options . created . push ( ( ) => {
41
- this . $watch ( '$metaInfo' , ( ) => triggerUpdate ( this ) )
30
+ ensuredPush ( this . $options , 'created' , ( ) => {
31
+ this . $watch ( '$metaInfo' , function ( ) {
32
+ triggerUpdate ( this , 'watcher' )
33
+ } )
42
34
} )
43
35
}
44
36
}
45
37
46
38
updateOnLifecycleHook . forEach ( ( lifecycleHook ) => {
47
- this . $options [ lifecycleHook ] = this . $options [ lifecycleHook ] || [ ]
48
- this . $options [ lifecycleHook ] . push ( ( ) => triggerUpdate ( this ) )
39
+ ensuredPush ( this . $options , lifecycleHook , ( ) => triggerUpdate ( this , lifecycleHook ) )
49
40
} )
50
41
51
42
// force an initial refresh on page load and prevent other lifecycleHooks
52
43
// to triggerUpdate until this initial refresh is finished
53
44
// this is to make sure that when a page is opened in an inactive tab which
54
45
// has throttled rAF/timers we still immeditately set the page title
55
- if ( isUndefined ( this . $root . _vueMetaInitialized ) ) {
56
- this . $root . _vueMetaInitialized = false
57
-
58
- this . $root . $options . mounted = this . $root . $options . mounted || [ ]
59
- this . $root . $options . mounted . push ( ( ) => {
60
- if ( ! this . $root . _vueMetaInitialized ) {
61
- this . $nextTick ( function ( ) {
62
- this . $root . $meta ( ) . refresh ( )
63
- this . $root . _vueMetaInitialized = true
64
- } )
65
- }
66
- } )
46
+ if ( isUndefined ( this . $root . _vueMetaPaused ) ) {
47
+ this . $root . _vueMetaInitialized = this . $isServer
48
+
49
+ if ( ! this . $root . _vueMetaInitialized ) {
50
+ ensuredPush ( this . $options , 'mounted' , ( ) => {
51
+ if ( ! this . $root . _vueMetaInitialized ) {
52
+ this . $nextTick ( function ( ) {
53
+ this . $root . $meta ( ) . refresh ( )
54
+ this . $root . _vueMetaInitialized = true
55
+ } )
56
+ }
57
+ } )
58
+ }
67
59
}
68
60
69
61
// do not trigger refresh on the server side
70
62
if ( ! this . $isServer ) {
71
63
// re-render meta data when returning from a child component to parent
72
- this . $options . destroyed = this . $options . destroyed || [ ]
73
- this . $options . destroyed . push ( ( ) => {
64
+ ensuredPush ( this . $options , 'destroyed' , ( ) => {
74
65
// Wait that element is hidden before refreshing meta tags (to support animations)
75
66
const interval = setInterval ( ( ) => {
76
67
if ( this . $el && this . $el . offsetParent !== null ) {
@@ -83,7 +74,7 @@ export default function createMixin(options) {
83
74
return
84
75
}
85
76
86
- triggerUpdate ( this )
77
+ triggerUpdate ( this , 'destroyed' )
87
78
} , 50 )
88
79
} )
89
80
}
0 commit comments