-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-ssr.tsx
115 lines (109 loc) · 3.11 KB
/
gatsby-ssr.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
import * as React from "react"
import { oneLine } from "common-tags"
import cssNanoMacro from "../macros/cssnano.macro"
import terserMacro from "../macros/terser.macro"
import { RenderBodyArgs } from "gatsby"
const generateHtml = (
str: string
): React.DOMAttributes<Element>["dangerouslySetInnerHTML"] => {
return {
__html: oneLine(str),
}
}
export function onRenderBody({ setHeadComponents }: RenderBodyArgs): void {
setHeadComponents([
<style
key="gatsby-image-style"
dangerouslySetInnerHTML={generateHtml(cssNanoMacro`
.gatsby-image-wrapper {
position: relative;
overflow: hidden;
}
.gatsby-image-wrapper picture.object-fit-polyfill {
position: static !important;
}
.gatsby-image-wrapper img {
bottom: 0;
height: 100%;
left: 0;
margin: 0;
max-width: none;
padding: 0;
position: absolute;
right: 0;
top: 0;
width: 100%;
object-fit: cover;
}
.gatsby-image-wrapper [data-main-image] {
opacity: 0;
transform: translateZ(0px);
transition: opacity 250ms linear;
will-change: opacity;
}
.gatsby-image-wrapper-constrained {
display: inline-block;
vertical-align: top;
}
`)}
/>,
<noscript
key="gatsby-image-style-noscript"
dangerouslySetInnerHTML={generateHtml(
`<style>` +
cssNanoMacro`
.gatsby-image-wrapper noscript [data-main-image] {
opacity: 1 !important;
}
.gatsby-image-wrapper [data-placeholder-image] {
opacity: 0 !important;
}
` +
`</style>`
)}
/>,
<script
key="gatsby-image-style-script"
type="module"
dangerouslySetInnerHTML={generateHtml(terserMacro`
const hasNativeLazyLoadSupport = typeof HTMLImageElement !== "undefined" && "loading" in HTMLImageElement.prototype;
if (hasNativeLazyLoadSupport) {
document.body.addEventListener('load', function gatsbyImageNativeLoader(e) {
const target = e.target;
// if image is not tagged with Main Image we bail
if (typeof target.dataset["mainImage"] === 'undefined') {
return
}
// if a main image does not have a ssr tag, we know it's not the first run anymore
if (typeof target.dataset["gatsbyImageSsr"] === 'undefined') {
return;
}
let imageWrapper = null;
let parentElement = target;
while (imageWrapper === null && parentElement) {
if (typeof parentElement.parentNode.dataset["gatsbyImageWrapper"] !== "undefined") {
imageWrapper = parentElement.parentNode;
}
parentElement = parentElement.parentNode;
}
const placeholder = imageWrapper.querySelector("[data-placeholder-image]");
const img = new Image();
img.src = target.currentSrc;
// We decode the img through javascript so we're sure the blur-up effect works
img.decode()
.catch(() => {
// do nothing
})
.then(() => {
target.style.opacity = 1;
if (placeholder) {
placeholder.style.opacity = 0;
placeholder.style.transition = "opacity 500ms linear";
}
})
}, true)
}
`)}
/>,
])
}