In this lesson, you will learn how to reduce your application’s initial startup time by strategically managing and loading large binary assets like high-resolution images, videos, and fonts.
The Problem: The "Heavy" Bundle
When you build your React Native app, Expo and the Metro bundler package your JavaScript code and local assets into a single binary bundle. If your project contains massive files (e.g., a 10MB background video or dozens of large PNGs), the app has to load these into memory before the first screen even appears. This leads to longer splash screen times and a sluggish user experience.
Strategy: Deferred Loading
To optimize this, you shouldn't bundle every asset inside your main application package. Instead, consider these three strategies:
- Lazy Loading: If an asset is only needed for a specific, secondary screen, don’t import it at the top level of your file. Instead, use local imports inside the component that requires them. This ensures the asset is only processed when that component actually renders.
- Asset Hosting (CDN): For very large binary files (like high-definition tutorials or long video clips), move them to a Content Delivery Network (CDN) like AWS S3 or Cloudinary. Instead of bundling the file locally, simply store the URL as a string in your app. Your app can then download the file on demand.
- Resolution-Specific Assets: Always provide scaled versions of your images. If you only serve an "extra-large" version of an icon to a small phone screen, you are wasting memory and bandwidth. Use standard naming conventions (
image.png,image@2x.png,image@3x.png) so that Expo’s bundler only includes the version appropriate for the specific device’s pixel density.
Example: On-Demand Loading
Instead of importing a large resource at the top of your file:
// AVOID: import largeVideo from './assets/heavy-video.mp4';
const VideoScreen = () => {
// DO: Load the resource only when this component is mounted
const largeVideo = require('./assets/heavy-video.mp4');
return <Video source={largeVideo} />;
};
By keeping your "initial bundle" lean, the app's JavaScript engine can execute faster, allowing your users to see their first UI elements almost immediately.
Key Takeaway: Never bundle assets that aren't strictly required for the initial launch; use lazy loading and remote hosting to keep your startup time fast and responsive.