Import On Visibility

patternsdev/skills/javascript/import-on-visibility

by patternsdev48bf58a488cdMIT253 starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated 5 months ago

Teaches visibility-based lazy loading using Intersection Observer. Use when you have below-the-fold components or images that don't need to load until the user scrolls to them.

Instructions onlySoftware Development
AI-generated overview

Guides visibility-based lazy loading of below-the-fold components and images using Intersection Observer.

What it does
This skill explains the import-on-visibility pattern for deferring components and images until they enter the viewport. It covers using the IntersectionObserver API or libraries such as react-lazyload and react-loadable-visibility, and providing a loading fallback while the module loads. It also lists cases where the pattern should not be applied, such as above-the-fold content or SEO-critical markup.
When to use it
Use it when a page has below-the-fold components, images, widgets, or heavy modules that do not need to load until the user scrolls to them. It is also relevant when reducing initial load time is a goal and a loading fallback can be shown.
Requirements
No scripts or packages are shipped; it is instructions only. Applying the pattern requires a JavaScript or TypeScript project and either the browser IntersectionObserver API or a library such as react-lazyload or react-loadable-visibility.

Import On Visibility

Besides user interaction, we often have components that aren't visible on the initial page. A good example of this is lazy loading images that aren't directly visible in the viewport, but only get loaded once the user scrolls down.

As we're not requesting all images instantly, we can reduce the initial loading time. We can do the same with components! In order to know whether components are currently in our viewport, we can use the IntersectionObserver API, or use libraries such as react-lazyload or react-loadable-visibility to quickly add import on visibility to our application.

When to Use

  • Use this for components that aren't visible on the initial page (e.g., below-the-fold content)
  • This is helpful for lazy loading images, widgets, or heavy components as the user scrolls

When NOT to Use

  • For above-the-fold content that must render immediately — deferring it causes visible layout shifts and slow LCP
  • When the component is lightweight enough that lazy loading adds more overhead than it saves
  • When the content is critical for SEO and needs to be present in the initial HTML

Instructions

  • Use the IntersectionObserver API to detect when components enter the viewport
  • Use libraries like react-lazyload or react-loadable-visibility for quick implementation
  • Provide a loading fallback component while the module is being loaded

Details

Whenever a component is rendered to the screen, react-loadable-visibility detects that the element should be visible on the screen. Only then, it will start importing the module while the user sees a loading component being rendered.

This fallback component lets the user know that our application hasn't frozen: they simply need to wait a short while for the module to be loaded, parsed, compiled, and executed!

Source

Source and attribution

Source:patternsdev/skillsinjavascript/import-on-visibilityat commit48bf58a

License: MIT

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal