Skip to content

Commit c25cf25

Browse files
authored
Fix lazy loading of images in CmsSlot. (#93)
1 parent 01c434c commit c25cf25

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/CmsSlot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const styles = theme => ({
2424
left: 0,
2525
},
2626
},
27-
'& img[data-rsf-lazy]': {
27+
'& img[data-src]': {
2828
visibility: 'hidden',
2929
},
3030
},

src/utils/lazyLoadImages.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44
* attribute is copied to `src`.
55
*
66
* See https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/
7+
* @param {DOMElement} element The img element to lazy load
8+
* @param {Object} options
9+
* @param {Object} options.lazySrcAttribute The attribute containing the image URL. Defaults to `data-src`.
710
*/
8-
export default function lazyLoadImages(element, { selector = 'img[data-rsf-lazy]' } = {}) {
11+
export default function lazyLoadImages(element, { lazySrcAttribute = 'data-src' } = {}) {
912
if (!element) return
10-
const lazyImages = [...element.querySelectorAll(selector)]
13+
const lazyImages = [...element.querySelectorAll(`img[${lazySrcAttribute}]`)]
1114
if (!lazyImages.length) return
1215

1316
let lazyImageObserver
1417

1518
const load = img => {
16-
img.src = img.dataset.src
19+
const src = img.getAttribute(lazySrcAttribute)
20+
const onload = () => {
21+
img.removeAttribute(lazySrcAttribute)
22+
img.removeEventListener('load', onload)
23+
}
24+
img.addEventListener('load', onload)
25+
img.setAttribute('src', src)
1726

1827
if (lazyImageObserver) {
1928
lazyImageObserver.disconnect(img)

test/utils/lazyLoadImages.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('lazyLoadImages', () => {
1212

1313
return (
1414
<div ref={ref}>
15-
<img ref={imageRef} data-src="https://via.placeholder.com/600x600" data-rsf-lazy="1" />
15+
<img ref={imageRef} data-src="https://via.placeholder.com/600x600" />
1616
</div>
1717
)
1818
}
@@ -38,7 +38,7 @@ describe('lazyLoadImages', () => {
3838
})
3939

4040
it('should do nothing if selector can not find anything', () => {
41-
selector = { selector: 'test' }
41+
selector = { lazySrcAttribute: 'data-lazy-src' }
4242

4343
wrapper = mount(<Test />)
4444

@@ -53,10 +53,10 @@ describe('lazyLoadImages', () => {
5353
// Not intersected
5454
observer.simulateChange(0, imageRef.current)
5555
expect(ref.current.src).toBeUndefined()
56-
5756
observer.simulateChange(1, imageRef.current)
58-
59-
expect(imageRef.current.src).toBe('https://via.placeholder.com/600x600')
57+
expect(imageRef.current.getAttribute('src')).toBe('https://via.placeholder.com/600x600')
58+
imageRef.current.dispatchEvent(new Event('load'))
59+
expect(imageRef.current.getAttribute('data-src')).toBe(null)
6060
})
6161

6262
describe('if intersection observer is not available', () => {

0 commit comments

Comments
 (0)