-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHydrate.js
190 lines (167 loc) · 4.35 KB
/
Hydrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* eslint-disable no-console */
import PropTypes from '@znck/prop-types'
const isBrowser = typeof window !== 'undefined'
const io =
typeof IntersectionObserver !== 'undefined'
? new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting || entry.intersectionRatio > 0) {
entry.target.dispatchEvent(new CustomEvent('hydration:visible'))
}
})
})
: null
const asyncFactoryResolved = { resolved: true }
const asyncFactory = {}
const clientSideDivData = {
attrs: {
'data-force-hydrate': true,
},
}
export default {
props: {
on: PropTypes.oneOfType(String, PropTypes.arrayOf(String)),
onClick: PropTypes.bool,
onHover: PropTypes.bool,
onInteraction: PropTypes.bool,
whenVisible: PropTypes.bool,
whenIdle: PropTypes.bool,
withDelay: PropTypes.number,
ssrOnly: PropTypes.bool,
force: PropTypes.bool,
},
data: () => ({
hydrated: !isBrowser,
}),
created() {
PropTypes.run(() => {
if (
!this.on &&
!this.onClick &&
!this.onHover &&
!this.onInteraction &&
!this.whenVisible &&
!this.whenIdle &&
!this.ssrOnly &&
this.force === undefined
) {
console.error(
`Select at least one trigger to enable hydration. If you don't want to hydrate at all use 'ssr-only'.`
)
}
if (this.withDelay && this.withDelay < 800) {
console.warn(
`Delay duration ${
this.withDelay
}ms is too low. A good choice would be around 2000ms. See https://github.com/znck/lazy-hydration.`
)
}
})
},
mounted() {
if (this.$el.dataset.forceHydrate) {
// No SSR rendered content. Render now.
this.hydrate()
return
}
if (this.ssrOnly) return
let withDelay
const on = (Array.isArray(this.on) ? this.on : [this.on])
.slice()
.filter(name => typeof name !== 'string')
if (this.onClick) {
on.push('click')
}
if (this.onHover || this.onInteraction) {
on.push('mouseenter')
if (this.onInteraction) {
on.push('focus')
}
}
if (this.whenIdle) {
if (typeof requestIdleCallback !== 'undefined') {
const id = requestIdleCallback(
() => {
requestAnimationFrame(() => {
this.hydrate()
})
},
{ timeout: 500 }
)
this.idle = () => cancelIdleCallback(id)
} else withDelay = 2000
}
if (this.whenVisible) {
const el = this.$el
// As root node does not have any box model, it cannot intersect.
on.push('hydration:visible')
if (io) io.observe(el)
else {
withDelay = 2000
PropTypes.run(() =>
console.warn('IntersectionObserver polyfill is required.')
)
}
this.visible = () => {
io && io.unobserve(el)
}
}
if (on.length) {
on.forEach(event =>
this.$el.addEventListener(event, this.hydrate, {
capture: true,
once: true,
})
)
this.off = () =>
on.forEach(event => this.$el.removeEventListener(event, this.hydrate))
}
if (this.withDelay || withDelay) {
const id = setTimeout(this.hydrate, this.withDelay || withDelay)
this.delay = () => clearTimeout(id)
}
},
beforeDestroy() {
this.cleanup()
},
methods: {
cleanup() {
const handlers = ['visible', 'idle', 'delay', 'off']
for (const handler of handlers) {
if (handler in this) {
this[handler]()
delete this[handler]
}
}
},
hydrate() {
this.hydrated = true
this.cleanup()
},
},
render(h) {
const firstChild = () => {
const children = this.$scopedSlots.default
? this.$scopedSlots.default({ hydrated: this.hydrated })
: this.$slots.default
return Array.isArray(children) ? children[0] : children
}
const vnode = this.hydrated
? firstChild()
: h('div', clientSideDivData)
vnode.asyncFactory = this.hydrated ? asyncFactoryResolved : asyncFactory
vnode.isComment = !this.hydrated
if (isBrowser) {
window.vnode = vnode
}
return vnode
},
watch: {
force: {
handler(value) {
if (value) this.hydrate()
},
immediate: true,
},
},
}