question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lazy-loaded srcset not updated

See original GitHub issue

Hi, I am using Swiper with lazy load functionality. When I update the images prop the srcset attribute does not get updated. Am I doing something wrong?

import React from 'react';
import styled, { withTheme, injectGlobal } from 'styled-components';
import Swiper from 'react-id-swiper';
import 'react-id-swiper/src/styles/css/swiper.css';

import { CLOUDINARY_HOST } from '../../config';

const SwiperLazyImage = styled.img`
  max-width: 100%;
`;

const StyledImage = ({ srcset }) => (
  <SwiperLazyImage className="swiper-lazy" data-srcset={srcset} sizes="100%" alt="" />
);

const Preloader = styled.div``;

class ImageCarousel extends React.Component {

  componentDidUpdate() {
    console.log(this.swiper);
    this.swiper.update();
  }

  render() {
    const { images } = this.props;
    const imageNodes = images.map((image, key) => {

      const imageTransforms = `
        ${CLOUDINARY_HOST}/w_320,ar_3:4,q_70,f_auto,c_fill${image} 320w,
        ${CLOUDINARY_HOST}/w_640,ar_3:4,q_70,f_auto,c_fill${image} 640w,
        ${CLOUDINARY_HOST}/w_960,ar_3:4,q_70,f_auto,c_fill${image} 960w,
        ${CLOUDINARY_HOST}/w_1280,ar_3:4,q_70,f_auto,c_fill${image} 1280w
      `;

      return (
        <div key={key}>
          <StyledImage srcset={imageTransforms} />
          <Preloader className="swiper-lazy-preloader" />
        </div>
      )}
    );

    const params = {
      slidesPerView: 1,
      preloadImages: false,
      lazy: {
        loadPrevNext: true,
      },
      pagination: {
        el: '.swiper-pagination',
      }
    };

    injectGlobal`
      .swiper-pagination-bullets { bottom: 0 !important; }
      .swiper-pagination-bullet-active { background: ${ this.props.theme.colors.tangerine } !important; }
    `;

    return (
      <Swiper ref={(node) => this.swiper = (node) ? node.swiper : null} {...params}>
        {imageNodes}
      </Swiper>
    );
  }
}

export default withTheme(ImageCarousel);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
ntucakoviccommented, Apr 4, 2019

I’ve read the lazy load implementation, it’s because there’s no check whether src is matching data-src.

If you update data-srcset (or data-src for that matter), you need to tell React that it’s a unique child compared to previous one (which it is).

So adding a key property that is unique to the original image URL would work. It will tell react to re-render the new image (and containing slide).

Other option is to implement useEffect to watch for image changes and call .update() on swiper instance. I also had the need to do swiperInstance.lazy.load(); when I change the images. (because first image for some reason won’t be downloaded, that’s due to lazy implementation and not the React fix we’re addressing with key property).

Just to help out someone looking through this issue, I see it’s quite stale.

0reactions
theskillwithincommented, May 23, 2020

if using gatsby #394

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to do lazy loading image with srcset? - Stack Overflow
As you can see the value in the src attribute is not modified. Read more at http://ivopetkov.com/b/lazy-load-responsive-images/.
Read more >
Lazy load changes srcset to data-srcset (unable to render)
Hi there, I noticed when the lazy load feature is used, images within an srcset do not render as the attribute is changed...
Read more >
Lazy load of responsive images with srcset and LazyLoad
It's now possible (yes, today!) to have lazy loading on responsive images to make our images to adapt to users screens and keep...
Read more >
Some images are not LazyLoaded - WP Rocket
Problem Some images are not lazyloaded PageSpeed lists some images under "defer offscreen images" If you've already ... data-srcset
Read more >
Browser-level image lazy loading for the web - web.dev
The new distance-from-viewport thresholds in Chrome loading 90KB of ... In most scenarios images still lazy-load if dimensions are not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found