Aljullu / react-lazy-load-image-component

React Component to lazy load images and components using a HOC to track window scroll position.
https://www.npmjs.com/package/react-lazy-load-image-component
MIT License
1.44k stars 109 forks source link

Is there any way to do server side render ? #55

Open krishnaTORQUE opened 4 years ago

krishnaTORQUE commented 4 years ago

Describe the solution you'd like Server Side Rending. Send IMG tag directly to client

vhpoet commented 4 years ago

I'd do a PR, but I'm not sure this is the most optimal place to put the noscript.

  1. Clone the repo

  2. Open LazyLoadImage.jsx

  3. Replace this with the below code.

    return (
      <>
        <LazyLoadComponent
          beforeLoad={beforeLoad}
          className={className}
          delayMethod={delayMethod}
          delayTime={delayTime}
          height={height}
          placeholder={placeholder}
          scrollPosition={scrollPosition}
          style={style}
          threshold={threshold}
          useIntersectionObserver={useIntersectionObserver}
          visibleByDefault={visibleByDefault}
          width={width}
        >
          {this.getImg()}
        </LazyLoadComponent>
        <noscript>{this.getImg()}</noscript>
      </>
    );
  4. Profit

Profit

vhpoet commented 4 years ago

Actually, this is better.

LazyLoadImage.jsx

if (typeof window === "undefined") return <noscript>{this.getImg()}</noscript>;

return (
  <>
    <LazyLoadComponent
      beforeLoad={beforeLoad}
      className={className}
      delayMethod={delayMethod}
      delayTime={delayTime}
      height={height}
      placeholder={placeholder}
      scrollPosition={scrollPosition}
      style={style}
      threshold={threshold}
      useIntersectionObserver={useIntersectionObserver}
      visibleByDefault={visibleByDefault}
      width={width}
    >
      {this.getImg()}
    </LazyLoadComponent>
  </>
);