twobin / react-lazyload

Lazy load your component, image or anything matters the performance.
MIT License
5.88k stars 488 forks source link

LazyLoad not rendering child component when JavaScript is disabled in the browser #415

Open diennn-gg opened 2 months ago

diennn-gg commented 2 months ago

I'm using the react-lazyload library to lazy-load some components in my React application. However, I've encountered an issue where the child component inside LazyLoad is not rendered when JavaScript is disabled in the browser.

When JavaScript is disabled, the The Component is not rendered. I'm aware that LazyLoad relies on JavaScript to detect when the component should be loaded, but I need to ensure that Component still renders even if JavaScript is turned off.

Is there a way to make LazyLoad render child components when JavaScript is disabled?

Thank?

100hell commented 2 months ago

Hi @diennn-gg, Unfortunately, the react-lazyload library, like most JavaScript-based lazy loading solutions, relies heavily on JavaScript to detect when a component should be rendered. When JavaScript is disabled, the library cannot execute the logic to load the component, resulting in the child component not being rendered.

Since React itself requires JavaScript to function, if JavaScript is disabled, the entire app won't work as expected. However, there are a few potential strategies you could use to ensure your component or important content still displays:

Solutions:

  1. Server-Side Rendering (SSR): If you need your content to be displayed even when JavaScript is disabled, you could implement Server-Side Rendering (SSR) using a framework like Next.js. This way, the content will be rendered on the server and served as plain HTML. While lazy loading still requires JavaScript, this ensures that essential content is rendered and visible on the initial page load, even without JS.

run : npm install next react react-dom

  1. Noscript Fallback: Another approach is to add a

Example: import LazyLoad from 'react-lazyload';

const MyComponent = () => (

);

This ensures that when JavaScript is disabled, the browser will still render the content inside the

  1. Progressive Enhancement: Design your site to deliver essential content without relying on JavaScript, ensuring that users with JS disabled still have access to basic functionality and content. You can load critical components without lazy loading them for non-JS users, while enhancing the experience for those with JS enabled by using react-lazyload.

Unfortunately, there isn't a direct way to make react-lazyload itself work without JavaScript, but these techniques can provide fallbacks or alternatives for non-JS users.