-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-image.browser.tsx
299 lines (266 loc) · 8.26 KB
/
gatsby-image.browser.tsx
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* eslint-disable no-unused-expressions */
import React, {
Component,
ElementType,
createRef,
MutableRefObject,
FunctionComponent,
ImgHTMLAttributes,
RefObject,
CSSProperties,
} from "react"
import {
getWrapperProps,
hasNativeLazyLoadSupport,
storeImageloaded,
hasImageLoaded,
gatsbyImageIsInstalled,
} from "./hooks"
import { PlaceholderProps } from "./placeholder"
import { MainImageProps } from "./main-image"
import { Layout } from "../image-utils"
import { getSizer } from "./layout-wrapper"
import { propTypes } from "./gatsby-image.server"
import { Unobserver } from "./intersection-observer"
import { render } from "react-dom"
// eslint-disable-next-line @typescript-eslint/naming-convention
export interface GatsbyImageProps
extends Omit<
ImgHTMLAttributes<HTMLImageElement>,
"placeholder" | "onLoad" | "src" | "srcSet" | "width" | "height"
> {
alt: string
as?: ElementType
className?: string
class?: string
imgClassName?: string
image: IGatsbyImageData
imgStyle?: CSSProperties
backgroundColor?: string
objectFit?: CSSProperties["objectFit"]
objectPosition?: CSSProperties["objectPosition"]
onLoad?: () => void
onError?: () => void
onStartLoad?: (props: { wasCached?: boolean }) => void
}
export interface IGatsbyImageData {
layout: Layout
width: number
height: number
backgroundColor?: string
images: Pick<MainImageProps, "sources" | "fallback">
placeholder?: Pick<PlaceholderProps, "sources" | "fallback">
}
class GatsbyImageHydrator extends Component<
GatsbyImageProps,
{ isLoading: boolean; isLoaded: boolean }
> {
root: RefObject<HTMLImageElement | undefined> = createRef<
HTMLImageElement | undefined
>()
hydrated: MutableRefObject<boolean> = { current: false }
forceRender: MutableRefObject<boolean> = {
// In dev we use render not hydrate, to avoid hydration warnings
current: process.env.NODE_ENV === `development`,
}
lazyHydrator: () => void | null = null
ref = createRef<HTMLImageElement>()
unobserveRef: Unobserver
constructor(props) {
super(props)
this.state = {
isLoading: hasNativeLazyLoadSupport(),
isLoaded: false,
}
}
_lazyHydrate(props, state): Promise<void> {
const hasSSRHtml = this.root.current.querySelector(
`[data-gatsby-image-ssr]`
)
// On first server hydration do nothing
if (hasNativeLazyLoadSupport() && hasSSRHtml && !this.hydrated.current) {
this.hydrated.current = true
return Promise.resolve()
}
return import(`./lazy-hydrate`).then(({ lazyHydrate }) => {
this.lazyHydrator = lazyHydrate(
{
image: props.image.images,
isLoading: state.isLoading,
isLoaded: state.isLoaded,
toggleIsLoaded: () => {
props.onLoad?.()
this.setState({
isLoaded: true,
})
},
ref: this.ref,
...props,
},
this.root,
this.hydrated,
this.forceRender
)
})
}
/**
* Choose if setupIntersectionObserver should use the image cache or not.
*/
_setupIntersectionObserver(useCache = true): void {
import(`./intersection-observer`).then(({ createIntersectionObserver }) => {
const intersectionObserver = createIntersectionObserver(() => {
if (this.root.current) {
const cacheKey = JSON.stringify(this.props.image.images)
this.props.onStartLoad?.({
wasCached: useCache && hasImageLoaded(cacheKey),
})
this.setState({
isLoading: true,
isLoaded: useCache && hasImageLoaded(cacheKey),
})
}
})
if (this.root.current) {
this.unobserveRef = intersectionObserver(this.root)
}
})
}
shouldComponentUpdate(nextProps, nextState): boolean {
let hasChanged = false
if (!this.state.isLoading && nextState.isLoading && !nextState.isLoaded) {
// Props have changed between SSR and hydration, so we need to force render instead of hydrate
this.forceRender.current = true
}
// this check mostly means people do not have the correct ref checks in place, we want to reset some state to suppport loading effects
if (this.props.image.images !== nextProps.image.images) {
// reset state, we'll rely on intersection observer to reload
if (this.unobserveRef) {
// unregister intersectionObserver
this.unobserveRef()
// // on unmount, make sure we cleanup
if (this.hydrated.current && this.lazyHydrator) {
render(null, this.root.current)
}
}
this.setState(
{
isLoading: false,
isLoaded: false,
},
() => {
this._setupIntersectionObserver(false)
}
)
hasChanged = true
}
if (this.root.current && !hasChanged) {
this._lazyHydrate(nextProps, nextState)
}
return false
}
componentDidMount(): void {
if (this.root.current) {
const ssrElement = this.root.current.querySelector(
`[data-gatsby-image-ssr]`
) as HTMLImageElement
const cacheKey = JSON.stringify(this.props.image.images)
// when SSR and native lazyload is supported we'll do nothing ;)
if (
hasNativeLazyLoadSupport() &&
ssrElement &&
gatsbyImageIsInstalled()
) {
this.props.onStartLoad?.({ wasCached: false })
// When the image is already loaded before we have hydrated, we trigger onLoad and cache the item
if (ssrElement.complete) {
this.props.onLoad?.()
storeImageloaded(cacheKey)
} else {
// We need the current class context (this) inside our named onLoad function
// The named function is necessary to easily remove the listener afterward.
// eslint-disable-next-line @typescript-eslint/no-this-alias
const _this = this
// add an onLoad to the image
ssrElement.addEventListener(`load`, function onLoad() {
ssrElement.removeEventListener(`load`, onLoad)
_this.props.onLoad?.()
storeImageloaded(cacheKey)
})
}
return
}
// Fallback to custom lazy loading (intersection observer)
this._setupIntersectionObserver(true)
}
}
componentWillUnmount(): void {
// Cleanup when onmount happens
if (this.unobserveRef) {
// unregister intersectionObserver
this.unobserveRef()
// on unmount, make sure we cleanup
if (this.hydrated.current && this.lazyHydrator) {
this.lazyHydrator()
}
}
return
}
render(): JSX.Element {
const Type = this.props.as || `div`
const { width, height, layout } = this.props.image
const {
style: wStyle,
className: wClass,
...wrapperProps
} = getWrapperProps(width, height, layout)
let className = this.props.className
// preact class
if (this.props.class) {
className = this.props.class
}
const sizer = getSizer(layout, width, height)
return (
<Type
{...wrapperProps}
style={{
...wStyle,
...this.props.style,
backgroundColor: this.props.backgroundColor,
}}
className={`${wClass}${className ? ` ${className}` : ``}`}
ref={this.root}
dangerouslySetInnerHTML={{
__html: sizer,
}}
suppressHydrationWarning
/>
)
}
}
export const GatsbyImage: FunctionComponent<GatsbyImageProps> =
function GatsbyImage(props) {
if (!props.image) {
if (process.env.NODE_ENV === `development`) {
console.warn(`[gatsby-plugin-image] Missing image prop`)
}
return null
}
if (!gatsbyImageIsInstalled() && process.env.NODE_ENV === `development`) {
console.warn(
`[gatsby-plugin-image] You're missing out on some cool performance features. Please add "gatsby-plugin-image" to your gatsby-config.js`
)
}
const { className, class: classSafe, backgroundColor, image } = props
const { width, height, layout } = image
const propsKey = JSON.stringify([
width,
height,
layout,
className,
classSafe,
backgroundColor,
])
return <GatsbyImageHydrator key={propsKey} {...props} />
}
GatsbyImage.propTypes = propTypes
GatsbyImage.displayName = `GatsbyImage`