Building a high-performance React app is easy when you have a 100Mbps connection. However, for users on 3G networks or unstable mobile data—common in many parts of Nepal—a standard 2MB React bundle can lead to a frustrating "white screen of death."
1. Code Splitting with React.lazy()
By default, Webpack bundles your entire app into one giant JavaScript file. Code splitting allows you to break that file into smaller chunks, loading them only when needed.
import React, { Suspense, lazy } from 'react';
const HeavyComponent = lazy(() => import('./HeavyComponent'));
function MyComponent() {
return (
<Suspense fallback={<div>Loading...</div>}>
<HeavyComponent />
</Suspense>
);
}
2. Perceived Speed: Skeleton Screens
On slow networks, psychological speed is just as important as technical speed. Instead of showing a blank screen or a generic loading spinner, use Skeleton Screens. They provide a visual cue of the content structure, making the app feel like it's loading instantly.
3. Asset Optimization
Images are often the heaviest part of a page. To optimize for low-bandwidth environments:
- Use WebP: It offers significantly better compression than PNG or JPEG.
- Image Lazy Loading: Use the native
loading="lazy"attribute for images below the fold. - Blur-up Technique: Show a tiny, 10px blurred version of an image while the high-res version downloads.
4. Intelligent Caching with React Query
Why fetch the same data twice? Using a library like @tanstack/react-query allows you to cache API responses. When a user navigates back to a page on a slow connection, they see the cached data immediately while the app fetches updates in the background.
Conclusion
Performance isn't just a technical metric; it's a feature. By reducing your bundle size and improving perceived performance, you ensure your React applications are accessible to everyone, regardless of their internet speed.