1
1
import { triggerUpdate } from '../client/update'
2
2
import { isUndefined , isFunction } from '../utils/is-type'
3
3
import { find } from '../utils/array'
4
+ import { getTag } from '../utils/elements'
4
5
import { rootConfigKey } from './constants'
5
6
import { hasMetaInfo } from './meta-helpers'
6
7
import { addNavGuards } from './nav-guards'
@@ -21,12 +22,6 @@ export default function createMixin (Vue, options) {
21
22
const $options = this . $options
22
23
const devtoolsEnabled = Vue . config . devtools
23
24
24
- if ( this === $root ) {
25
- this . $on ( 'hook:beforeMount' , function ( ) {
26
- wasServerRendered = this . $el && this . $el . nodeType === 1 && this . $el . hasAttribute ( 'data-server-rendered' )
27
- } )
28
- }
29
-
30
25
Object . defineProperty ( this , '_hasMetaInfo' , {
31
26
configurable : true ,
32
27
get ( ) {
@@ -39,6 +34,22 @@ export default function createMixin (Vue, options) {
39
34
}
40
35
} )
41
36
37
+ if ( this === $root ) {
38
+ $root . $once ( 'hook:beforeMount' , function ( ) {
39
+ wasServerRendered = this . $el && this . $el . nodeType === 1 && this . $el . hasAttribute ( 'data-server-rendered' )
40
+
41
+ // In most cases when you have a SSR app it will be the first app thats gonna be
42
+ // initiated, if we cant detect the data-server-rendered attribute from Vue but we
43
+ // do see our own ssrAttribute then _assume_ the Vue app with appId 1 is the ssr app
44
+ // attempted fix for #404 & #562, but we rly need to refactor how we pass appIds from
45
+ // ssr to the client
46
+ if ( ! wasServerRendered && $root [ rootConfigKey ] && $root [ rootConfigKey ] . appId === 1 ) {
47
+ const htmlTag = getTag ( { } , 'html' )
48
+ wasServerRendered = htmlTag && htmlTag . hasAttribute ( options . ssrAttribute )
49
+ }
50
+ } )
51
+ }
52
+
42
53
// Add a marker to know if it uses metaInfo
43
54
// _vnode is used to know that it's attached to a real component
44
55
// useful if we use some mixin to add some meta tags (like nuxt-i18n)
@@ -107,6 +118,7 @@ export default function createMixin (Vue, options) {
107
118
108
119
this . $on ( 'hook:beforeMount' , function ( ) {
109
120
const $root = this [ rootKey ]
121
+
110
122
// if this Vue-app was server rendered, set the appId to 'ssr'
111
123
// only one SSR app per page is supported
112
124
if ( wasServerRendered ) {
@@ -119,35 +131,37 @@ export default function createMixin (Vue, options) {
119
131
this . $on ( 'hook:mounted' , function ( ) {
120
132
const $root = this [ rootKey ]
121
133
122
- if ( ! $root [ rootConfigKey ] . initialized ) {
123
- // used in triggerUpdate to check if a change was triggered
124
- // during initialization
125
- $root [ rootConfigKey ] . initializing = true
126
-
127
- // refresh meta in nextTick so all child components have loaded
128
- this . $nextTick ( function ( ) {
129
- const { tags, metaInfo } = $root . $meta ( ) . refresh ( )
130
-
131
- // After ssr hydration (identifier by tags === false) check
132
- // if initialized was set to null in triggerUpdate. That'd mean
133
- // that during initilazation changes where triggered which need
134
- // to be applied OR a metaInfo watcher was triggered before the
135
- // current hook was called
136
- // (during initialization all changes are blocked)
137
- if ( tags === false && $root [ rootConfigKey ] . initialized === null ) {
138
- this . $nextTick ( ( ) => triggerUpdate ( options , $root , 'init' ) )
139
- }
140
-
141
- $root [ rootConfigKey ] . initialized = true
142
- delete $root [ rootConfigKey ] . initializing
143
-
144
- // add the navigation guards if they havent been added yet
145
- // they are needed for the afterNavigation callback
146
- if ( ! options . refreshOnceOnNavigation && metaInfo . afterNavigation ) {
147
- addNavGuards ( $root )
148
- }
149
- } )
134
+ if ( $root [ rootConfigKey ] . initialized ) {
135
+ return
150
136
}
137
+
138
+ // used in triggerUpdate to check if a change was triggered
139
+ // during initialization
140
+ $root [ rootConfigKey ] . initializing = true
141
+
142
+ // refresh meta in nextTick so all child components have loaded
143
+ this . $nextTick ( function ( ) {
144
+ const { tags, metaInfo } = $root . $meta ( ) . refresh ( )
145
+
146
+ // After ssr hydration (identifier by tags === false) check
147
+ // if initialized was set to null in triggerUpdate. That'd mean
148
+ // that during initilazation changes where triggered which need
149
+ // to be applied OR a metaInfo watcher was triggered before the
150
+ // current hook was called
151
+ // (during initialization all changes are blocked)
152
+ if ( tags === false && $root [ rootConfigKey ] . initialized === null ) {
153
+ this . $nextTick ( ( ) => triggerUpdate ( options , $root , 'init' ) )
154
+ }
155
+
156
+ $root [ rootConfigKey ] . initialized = true
157
+ delete $root [ rootConfigKey ] . initializing
158
+
159
+ // add the navigation guards if they havent been added yet
160
+ // they are needed for the afterNavigation callback
161
+ if ( ! options . refreshOnceOnNavigation && metaInfo . afterNavigation ) {
162
+ addNavGuards ( $root )
163
+ }
164
+ } )
151
165
} )
152
166
// add the navigation guards if requested
153
167
if ( options . refreshOnceOnNavigation ) {
0 commit comments