When Lazy Loading Is Not Enough: Rethinking Performance in Static Next.js Applications
What actually needs to be loaded first?
Making the loading decision explicit
Different features need different loading conditions
What happens when the page keeps growing?
Making the solution maintainable
Avoiding another source of unnecessary dependencies
What if the deferred request fails?
How do we know the first load actually improved?
Using the package in another project
The trade-off is still there
Imagine this situation. You are working on a Next.js project configured to generate fully static pages. The decision has already been made, and changing the architecture is not necessarily an option. You have limited room to accommodate requirements that would otherwise benefit from server-side processing at request time.
Now imagine that the client expects excellent Google page performance scores, cares about SEO, and at the same time keeps requesting new features. Many of these features have to be implemented on the client side because of the existing configuration.
There is nothing particularly wrong with any of these requirements individually. The problem is what happens when they accumulate. Every new feature brings its own components, logic and potentially additional dependencies. At some point, the amount of JavaScript the browser has to download and execute during the first load becomes difficult to justify.
The performance score starts dropping, the client becomes frustrated, and you are asked to find a solution without removing functionality or changing the architecture.
So you start looking. You investigate lazy loading, dynamic imports and ways of delaying rendering. Some of these approaches work well, but in the particular static export setup I was working with, the ones I tried did not give me enough control over when the JavaScript was actually requested. Postponing the rendering of a component is of little help to the initial download if its code has already been fetched.
This was the situation I found myself in. Rather than trying to make every feature load faster at the same time, I started questioning whether all those features needed to load at all when a user first opened the page.
What actually needs to be loaded first?
Think of a modal that opens only after a user clicks a button. Why should its entire implementation be downloaded when the user lands on the page? The user might never open it.
The same question applies to a heavy chart far below the visible area, a complex form inside an unopened tab, or a widget that becomes relevant only after a particular interaction.
I wanted to keep the functionality available without making every visitor pay its initial loading cost. More importantly, I wanted the application to decide when that cost was justified.
This meant separating the code of selected components from the initial bundle where possible, and invoking their imports only when they became relevant. A modal could load after a click. A chart could load as the user approached it. A feature dependent on another operation could wait until that operation completed.
That is what led me to develop react-view-import.
Making the loading decision explicit
Dynamic imports allow a bundler to create separate JavaScript chunks. However, creating a separate chunk and deciding when to request it are two different responsibilities.
With react-view-import, the application supplies a function containing a dynamic import. The bundler can generate a separate chunk and retain the information needed to locate it. The package does not invoke that function until the configured loading condition is satisfied.
Until then, the application can display a placeholder. For viewport-based loading, the package uses the browser's IntersectionObserver API to monitor the placeholder's position.
Going back to the example of a heavy chart further down the page, this is how I can integrate it without immediately invoking its import:
Here, importer defines how the module is retrieved, while select identifies the React component exported by that module. The componentProps object contains the properties that will be passed to the loaded component.
The important detail is that the import is passed as a function rather than executed immediately. Statically importing the same component into the initial dependency graph could defeat the intended separation.
Once the loading condition is met, the import runs. After the module becomes available, the selected component takes the place of the placeholder.
The exact chunk boundaries and any framework-generated preloading remain the responsibility of the build system. The package controls when it invokes the supplied importer, not every possible network request made by the framework.
Different features need different loading conditions
The chart is only one of the problems in our original scenario. The modal, for example, should not depend on scrolling at all. Other components might need to begin loading as soon as the parent mounts, even though I want their code split from the initial bundle. This is why I did not want the package to be limited to viewport-based loading.
The package provides three strategies:
Viewport-based loading is the default. The import runs when the observed element meets the visibility criteria, including when it is already visible on mount.
Mount-based loading, enabled through loadOnMount, starts the import when the parent mounts.
Condition-based loading, configured through loadOnCondition, allows the application to decide when the import should run using a boolean value.
For example, a reviews section might belong to a tab that the user has not opened yet. Its code does not need to be requested simply because the rest of the page is ready.
When multiple loading options are configured, the package resolves them in the following order: loadOnMount, loadOnCondition, and finally viewport-based loading.
Internally, I used an enum-keyed lookup map to resolve the strategy instead of maintaining a chain of conditional statements. This keeps strategy selection separate from the loading implementation and makes the design easier to extend.
Viewport behavior can also be adjusted through rootMargin and threshold. Their defaults are 1000px 0px and 0.5, respectively. The margin expands the observation area so that fetching can begin before the component reaches the visible viewport. The threshold determines the intersection ratio required within that observation area.
What happens when the page keeps growing?
As the client continues to request functionality, the page might end up containing several expensive sections. Loading decisions should remain independent rather than forcing all of these sections to become ready together. Each instance of UILazyInView manages one component, which allows the parent to coordinate loading without putting all the imports into a single operation.
The following example maintains a collection of section indexes whose loading conditions have been activated:
As sections are encountered, their viewport callbacks update the parent state, enabling the corresponding imports. The application can extend this pattern for progressively loaded pages or infinite-scroll interfaces.
This example does not enforce strictly serial network requests: several sections may become eligible at the same time. If the application requires one import to finish before another starts, that dependency must be represented in its loading conditions.
Making the solution maintainable
An earlier version of the package identified named exports using a string:
The drawback was that an incorrect export name could remain undetected until runtime. I wanted component selection to benefit from the same type checking and editor navigation available elsewhere in a TypeScript application.
The typed selector provides that alternative:
When the imported module is correctly typed, TypeScript can identify an invalid property before the application runs. Developers can also navigate to the selected export through their editor.
The original exportName option remains available for existing integrations. The package's types make the two options mutually exclusive, so consumers choose either the string-based export name or the typed selector.
Avoiding another source of unnecessary dependencies
I initially used an external intersection-observer library but eventually replaced it with a small internal implementation. For a package whose purpose is to reduce unnecessary client-side code loading, I wanted to avoid introducing additional runtime dependencies.
React and React DOM are peer dependencies, with support starting at version 17. The package provides both ESM and CommonJS builds, together with TypeScript declarations, for use in environments such as Next.js, Vite and Create React App.
The viewport implementation relies on native IntersectionObserver support. If the API is unavailable where required, the package reports an explicit error rather than silently changing its loading behavior.
What if the deferred request fails?
A separate JavaScript chunk is still a network resource, and a request can fail. Different applications may need different responses to such failures, so I chose not to impose a built-in error interface or recovery strategy.
The application can wrap the dynamic import, update its own error state and retain the original rejection:
The placeholder remains available while the real component has not loaded. In this example, the application switches from a skeleton to its own fallback when the import fails. Rethrowing preserves the rejection rather than treating the failed import as successful.
Using a placeholder with dimensions close to those of the final component can also help reduce layout shifts when the component appears.
How do we know the first load actually improved?
The existence of a separate chunk in the build output is not enough to establish that its network request was deferred. I use the browser's Network panel to inspect when JavaScript resources are actually requested.
Open DevTools before navigating to the page, select the JavaScript filter and disable the cache for the test. Reload the page and examine the initial requests. Then scroll toward a deferred section or activate an interaction that enables a custom loading condition.
A chunk that has not already been requested or cached should appear when the corresponding importer runs. The request may occur before an element becomes visible because of the configured observation margin.
Comparing the initial requests with those triggered later provides a way to verify the loading behavior in the actual production build. To assess performance rather than only request timing, compare Lighthouse results and initial JavaScript transfer before and after the change under equivalent test conditions. It also helps identify chunks that the framework may have preloaded or modules that were included through another dependency.
Using the package in another project
The package is available on npm:
It is distributed under the MIT license.
The trade-off is still there
Going back to the original situation, the client can keep the features and the application can remain statically exported. What changes is when selected pieces of functionality are requested and executed.
This does not eliminate their cost. A user who opens a deferred modal may have to wait for its code to arrive. A chart loaded too late might become visible before it is ready. That is why choosing the right condition, observation margin and placeholder matters.
Nor would I defer every component. Content important to the first render, accessibility or search visibility should receive particular attention. Static export does not mean all content must be rendered exclusively in the browser, and the loading strategy should not undermine the reasons for choosing static pages in the first place.
The actual performance benefit needs to be measured against the original production build, including initial JavaScript transfer, main-thread work and the experience of users who interact with deferred features. A smaller initial workload is an objective, not a guarantee of a particular Lighthouse score.
The lesson I took from this project is that performance is not always about making everything faster. Sometimes it is about identifying what does not need to happen yet, and making sure it does not happen until there is a reason for it.



