Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy loading in Image component from react-datocms not work #17

Open
nephlin7 opened this issue Dec 15, 2020 · 2 comments
Open

lazy loading in Image component from react-datocms not work #17

nephlin7 opened this issue Dec 15, 2020 · 2 comments

Comments

@nephlin7
Copy link

I'm using NextJS v10 with DatoCMS. Currently, I want to use this plugin to improve the bundle sizes but I realized that the lazy loading in the Image component from https://github.com/datocms/react-datocms not work.

@jesster2k10
Copy link

I had a similar issue using a custom setup with LazySizes.
My code looked something like this:

const LazyImage = ({
  src,
  placeholderSrc,
  animationType = 'blur',
  lazyMinHeight,
  lazyMaxWidth,
  ...props
}: LazyImageProps) => (
  <div
    className={animationType === 'blur' && 'blur-up-container'}
    css={{ width: '100%', minHeight: lazyMinHeight }}
  >
    <Img
      {...props}
      className={cs(
        'lazyload',
        'js-only',
        animationType === 'blur' && 'blur-up',
      )}
      data-src={src}
      src={placeholderSrc}
      css={{
        '&.lazyload': {
          width: '100%',
          minHeight: lazyMinHeight,
        },
      }}
    />
    <noscript>
      <Img {...props} src={src} />
    </noscript>
  </div>
);

Where I was basically hiding the lazy loaded image using a .js-only tag (since it only works with JS enabled).
The issue lies with preact somehow thinking that javascript was disabled, when it was actually disabled and therefore evaluating this:

<noscript>
          <style>{`
          .js-only {
            display: none;
          }
        `}</style>
        </noscript>

Which caused my images to be hidden.
To resolve this, I just had to avoid rendering that piece of code on the client. So that was changed to:

{typeof window === 'undefined' && (
        <noscript>
          <style>{`
          .js-only {
            display: none;
          }
        `}</style>
        </noscript>
      )}

You would want to check out this issue for reference.
preactjs/preact#2797

@sventschui
Copy link
Member

@jesster2k10 preact indeed differs a bit from React's handling of noscript tags.

@nephlin7 if you provide a reproduction case we will be able to look into your issue. I am not familiar with react-datocms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants