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.

SSR, image not rendered from server

See original GitHub issue

Bug description The lazy loading is working perfectly on client side but none of my image are rendered from the server on the original loading SSR. Is there a special config to change in order for the image to be rendered as SSR? Stack is Meteor/React/Apllo/Styled Component

<StyledLazyLoadImage
  alt={`product image ${name}`}
  scrollPosition={scrollPosition}
  src={imgUrl200 ? imgUrl200 : imgUrl}
/>
const StyledLazyLoadImage = styled(LazyLoadImage)`
  margin: 0.5rem;
  max-width: 18rem;
  max-height: 22rem;
`;

Also tried without the scrollPosition but result is the same.

Expected behavior Was expecting the image to be rendered on server side.

Screenshots image

Technical details:

  • Package version: 1.5.1
  • Server Side Rendering? Yes
  • Device: Desktop
  • Operating System: MacOs
  • Browser: Chrome

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

1reaction
petr-glaser-deltatrecommented, Jan 13, 2022

Found the issue. The library is SSR friendly in a way it does not crash. So the flow on SSR with disabled JS is this:

  • Start render on server
  • PlaceholderWithoutTracking is rendered and !supportsObserver is true
  • updateVisibility is called, but typeof window === 'undefined' is true, so false is returned
  • SSR renders placeholder, default placeholder is <span>

Solution is to use placeholder={<img src={src} loading="lazy" />} which will keep the image rendered even on SSR with JS disabled.

0reactions
ItsRyanWucommented, Jul 6, 2022

Found the issue. The library is SSR friendly in a way it does not crash. So the flow on SSR with disabled JS is this:

  • Start render on server
  • PlaceholderWithoutTracking is rendered and !supportsObserver is true
  • updateVisibility is called, but typeof window === 'undefined' is true, so false is returned
  • SSR renders placeholder, default placeholder is <span>

Solution is to use placeholder={<img src={src} loading="lazy" />} which will keep the image rendered even on SSR with JS disabled.

I think an even better solution is we could wrap the <img/> inside the placeholder property with <noscript> tag, so it’s will become only readable to the search engine and won’t be executed by browsers in normal cases. I personally think it’s a good way to improve performance when it’s being parsed by browsers, as the placeholder is not really needed if you already set placeholderSrc property. So the final answer from me would be like this:

placeholder={
  <span>
    <noscript>
      {/* eslint-disable-next-line @next/next/no-img-element */}
      <img src={src} srcSet={srcSet} sizes={sizes} alt="placeholder" />
    </noscript>
  </span>
}

To be noticed that a <span/> or other non <noscript/> tags is required here to be a root node to prevent <LazyLoadImage/> from being failed to mount the real image element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image is not loading in server side rendering with react
I'm doing server-side rendering with React DOM Server using Create React App. I'm trying to load a static image but it's not showing...
Read more >
React16 Server Side Rendering (SSR) CSS and Images Not ...
Before looking into React16 SSR css and images related issues first check go through a Step By Step guide for implementing React16 Server...
Read more >
Image not rendering in SSRS MTHML format - MSDN - Microsoft
An image in SSRS report is not rendered properly for save as MHTML format in IE. Image is an external image. When i...
Read more >
Improve app performance with React server-side rendering
React server-side rendering offers performance benefits vs. client-side ... I would also like to address when SSR React does not make sense.
Read more >
What is server-side rendering and how does it improve site ...
Server -side rendering (SSR) addresses the performance and search engine ... No server is involved in the process, except to store the ...
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