File tree Expand file tree Collapse file tree 1 file changed +21
-16
lines changed
packages/gatsby-image/src Expand file tree Collapse file tree 1 file changed +21
-16
lines changed Original file line number Diff line number Diff line change @@ -36,27 +36,32 @@ const inImageCache = props => {
3636
3737let io
3838const listeners = [ ]
39- if ( typeof window !== `undefined` && window . IntersectionObserver ) {
40- io = new window . IntersectionObserver (
41- entries => {
42- entries . forEach ( entry => {
43- listeners . forEach ( l => {
44- if ( l [ 0 ] === entry . target ) {
45- // Edge doesn't currently support isIntersecting, so also test for an intersectionRatio > 0
46- if ( entry . isIntersecting || entry . intersectionRatio > 0 ) {
47- io . unobserve ( l [ 0 ] )
48- l [ 1 ] ( )
39+
40+ function getIO ( ) {
41+ if ( typeof io === `undefined` && typeof window !== `undefined` && window . IntersectionObserver ) {
42+ io = new window . IntersectionObserver (
43+ entries => {
44+ entries . forEach ( entry => {
45+ listeners . forEach ( l => {
46+ if ( l [ 0 ] === entry . target ) {
47+ // Edge doesn't currently support isIntersecting, so also test for an intersectionRatio > 0
48+ if ( entry . isIntersecting || entry . intersectionRatio > 0 ) {
49+ io . unobserve ( l [ 0 ] )
50+ l [ 1 ] ( )
51+ }
4952 }
50- }
53+ } )
5154 } )
52- } )
53- } ,
54- { rootMargin : `200px` }
55- )
55+ } ,
56+ { rootMargin : `200px` }
57+ )
58+ }
59+
60+ return io
5661}
5762
5863const listenToIntersections = ( el , cb ) => {
59- io . observe ( el )
64+ getIO ( ) . observe ( el )
6065 listeners . push ( [ el , cb ] )
6166}
6267
You can’t perform that action at this time.
0 commit comments