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

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

Open
diennn-gg opened this issue Sep 13, 2024 · 1 comment

Comments

@diennn-gg
Copy link

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
Copy link

100hell commented Sep 13, 2024

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 fallback that renders the content when JavaScript is disabled. This will not lazy-load the content, but it can ensure that the content is available.

Example:
import LazyLoad from 'react-lazyload';

const MyComponent = () => (

);

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

  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.

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

2 participants