My next project needed a fully static output, but most of its work happened on the client side. The client judged the site by its Google page-performance score, so the first load had to stay as light as possible.

The usual lazy-loading options did not give me what I needed. In my static export setup, a lazily loaded component still weighed on the first load. I wanted something more direct: the component is excluded from the main chunk, the site manifest keeps only a reference to it, and the chunk is fetched through a network request when a condition is met.

I could not find a solution that did this, so I built react-view-import. It is standalone, with no runtime dependencies. It uses only the React that your app already has.

What it does

  1. You write the import as a function, () => import("./HeavyChart"). The build puts the component into its own chunk and leaves only a reference in the site manifest.
  2. On the page, the package renders a placeholder and watches it with an IntersectionObserver.
  3. When the condition is true, the package runs the import, the browser fetches the chunk, and the real component replaces the placeholder.

You decide which elements are worth excluding from the first load, and the package takes care of when to fetch them.

Basic usage

import { UILazyInView } from "react-view-import"; export default function Page() { return ( <UILazyInView importer={() => import("./HeavyChart")} select={(module) => module.HeavyChart} componentProps={{ data: chartData }} placeholder={<div>Loading chart...</div>} /> ); } 
  • importer is the dynamic import. A normal static import would put the component back into the main chunk.
  • select picks the component from the module.
  • componentProps are passed to the component after it loads.
  • placeholder is shown until the chunk arrives.

The condition decides when the chunk loads

The loading is driven by a condition, and the package supports three:

  1. Viewport (default): the chunk loads when the placeholder enters the viewport, or at once if it is already visible on mount.
  2. On mount (loadOnMount): the chunk loads as soon as the parent mounts, for elements you want ready on load.
  3. Custom (loadOnCondition): the chunk loads when your own boolean becomes true, for example after a click, a hover, or when another element has finished loading.
<UILazyInView importer={() => import("./Reviews")} select={(module) => module.Reviews} componentProps={{}} loadOnCondition={hasOpenedTab} /> 

When more than one is set, they apply in a fixed priority: loadOnMount, then loadOnCondition, then the viewport. Inside the package, the strategy is picked from a lookup map keyed by an enum, not from chained if statements, so adding a strategy is a one-line change.

Two options tune the viewport trigger. rootMargin (default 1000px 0px) starts the fetch before the element is visible, and threshold (default 0.5) sets how much of it must be visible.

Lists and sequential loading

The package handles one element per component, so for a list you render one for each item. Each item has its own condition, and an item can trigger the next one:

import { useState, useCallback } from "react"; import { UILazyInView } from "react-view-import"; export const Sections = ({ sections }) => { const [loadedIndexes, setLoadedIndexes] = useState<number[]>([]); const handleInView = useCallback((index: number) => { setLoadedIndexes((previous) => previous.includes(index) ? previous : [...previous, index], ); }, []); return ( <div> {sections.map(({ title, index, importer }) => ( <UILazyInView key={index} importer={importer} select={(module) => module.default} componentProps={{ title }} loadOnCondition={loadedIndexes.includes(index)} onInView={() => handleInView(index)} placeholder={<div>Loading {title}...</div>} /> ))} </div> ); }; 

As the user scrolls, items enter the viewport and load their chunks one after another, which supports sequential loading and infinite-scroll style lists.

Type-safe selection

My first version chose the export with a string:

exportName="PhoneField" // a typo fails only at runtime 

The latest release adds a typed selector, which TypeScript checks and editors can follow as a real symbol:

select={(module) => module.PhoneField} // a typo fails at compile time 

exportName still works, so existing code does not need to change. You use one of the two, not both, and the types enforce it.

Small decisions

  • No runtime dependencies. An early version used an external intersection-observer library. I replaced it with a small local component, so the package needs only React and React DOM (version 17 or newer) as peers.
  • Both module formats. It ships as ESM and CommonJS with type definitions, so it works in Next.js, Vite, and Create React App.
  • One limit. It relies on the browser's built-in IntersectionObserver. Where it does not exist, the component throws a clear error and does not fail silently.

When a chunk fails to load

The package does not catch import failures. That choice stays with your app, so you decide what the user sees. Wrap the importer with a try/catch, set your own state, and rethrow, so the error still shows in the console as a log. Until the component loads, the placeholder stays on the page, so a skeleton that matches the final layout keeps the page from looking broken.

const [hasFailed, setHasFailed] = useState(false); <UILazyInView importer={async () => { try { return await import("./HeavyChart"); } catch (error) { setHasFailed(true); throw error; } }} select={(module) => module.HeavyChart} componentProps={{ data: chartData }} placeholder={hasFailed ? <ChartFallback /> : <ChartSkeleton />} /> 

See it in the network tab

  1. Open a page that uses the package and wait until it has fully loaded.
  2. Open DevTools (F12), go to the Network tab, and click the JS filter.
  3. Click the Clear button, so the list is empty.
  4. Now scroll down the page. New rows appear in the list as the elements come near the viewport. Each row is a chunk that was not in the main chunk, fetched through its own network request when its condition became true.
  5. Clear the list again and trigger another condition, such as a click or moving the mouse out of the page through the top edge. The chunk of the element that depends on it shows up in the list at that moment.

Try it

npm install react-view-import 

MIT-licensed.