Skip to content

Commit 0d93684

Browse files
authored
fix (EWWW compatibility): Terminate observer after first mutation (#3415)
1 parent 87dbc74 commit 0d93684

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/block-components/image/image-optimizer-polyfill.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ImageOptimizerPolyfill {
1212
init = () => {
1313
const imgs = document.querySelectorAll( '.stk-block img' )
1414

15+
let updateCount = 0
1516
// Use Mutation Observer because the src and/or srcset attributes may change if using dynamic content
1617
const MO = new MutationObserver( mutations => {
1718
mutations.forEach( mutation => {
@@ -33,12 +34,24 @@ class ImageOptimizerPolyfill {
3334
img.setAttribute( 'srcset', srcset )
3435
}
3536

37+
if ( img.getAttribute( 'data-src' ).indexOf( '&fit' ) !== -1 ) {
38+
const src = img.getAttribute( 'data-src' )
39+
const index = src.indexOf( '&fit' )
40+
const newSrc = src.slice( 0, index )
41+
img.setAttribute( 'data-src', newSrc )
42+
}
43+
3644
if ( img.getAttribute( 'src' ).indexOf( '&fit' ) !== -1 ) {
3745
const src = img.getAttribute( 'src' )
3846
const index = src.indexOf( '&fit' )
3947
const newSrc = src.slice( 0, index )
4048
img.setAttribute( 'src', newSrc )
4149
}
50+
51+
updateCount++
52+
if ( updateCount === imgs.length ) {
53+
MO.disconnect()
54+
}
4255
} )
4356
} )
4457

0 commit comments

Comments
 (0)