-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPerformanceWarnings.ts
More file actions
52 lines (46 loc) · 2.12 KB
/
PerformanceWarnings.ts
File metadata and controls
52 lines (46 loc) · 2.12 KB
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
const OVERSIZE_IMAGE_TOLERANCE = 500
let performanceObserver: PerformanceObserver
export const warnOnOversizedImage = (image: HTMLImageElement) => {
const { clientWidth, clientHeight, naturalWidth, naturalHeight } = image
if (
naturalWidth > clientWidth + OVERSIZE_IMAGE_TOLERANCE ||
naturalHeight > clientHeight + OVERSIZE_IMAGE_TOLERANCE
) {
console.warn(
`An image with URL ${image.src} has dimensions significantly smaller than intrinsic size, ` +
`and may slow down page load. You may address this by supplying a height and width for the image, ` +
`or by using the responsive image plugin. ` +
`Rendered size: ${clientWidth}x${clientHeight}. Intrinsic size: ${naturalWidth}x${naturalHeight}. ` +
`This warning can be surpressed by adding the 'silence-warnings' attribute to AdvancedImage.`
)
}
}
export const warnOnLazyLCP = (imgRef: HTMLImageElement) => {
if (
!performanceObserver &&
typeof window !== 'undefined' &&
window.PerformanceObserver
) {
performanceObserver = new PerformanceObserver((entryList) => {
const entries = entryList.getEntries()
if (entries.length === 0) {
return
}
// The final LCP entry is the only one that can be the real LCP element. This cast is safe because
// this performanceObserver callback only listens for largest-contentful-paint events.
const lcpCandidate = entries[entries.length - 1] as LargestContentfulPaint
if (lcpCandidate.element?.getAttribute('loading') === 'lazy') {
console.warn(
`An image with URL ${imgRef.src} has ' loading="lazy"' and has also been detected to be a possible ` +
`LCP element (https://web.dev/lcp). This can have a significant negative impact on page loading performance. ` +
`To fix this issue, remove, 'loading="lazy"' from images which may render in the initial viewport. ` +
`This warning can be surpressed by adding the 'silence-warnings' attribute to AdvancedImage.`
)
}
})
performanceObserver.observe({
type: 'largest-contentful-paint',
buffered: true
})
}
}