Loading...
Loading...
Use when loading all data upfront. Use when initial page load is slow. Use when fetching data that might not be needed.
npx skill4agent add yanko-belov/code-craft lazy-loadingNEVER load data until it's actually needed.// ❌ VIOLATION: Load everything upfront
async function loadDashboard(userId: string) {
const [
profile,
preferences,
notifications, // User might not check
recentActivity, // Collapsed by default
analytics, // Expensive, rarely viewed
recommendations, // Below the fold
fullHistory // Paginated anyway
] = await Promise.all([
fetchProfile(userId),
fetchPreferences(userId),
fetchNotifications(userId),
fetchRecentActivity(userId),
fetchAnalytics(userId), // Takes 2 seconds!
fetchRecommendations(userId),
fetchFullHistory(userId) // 10MB of data!
]);
return { profile, preferences, notifications, ... };
}// ✅ CORRECT: Load critical data first, rest on demand
// Initial load - only what's immediately visible
async function loadDashboard(userId: string) {
const [profile, preferences] = await Promise.all([
fetchProfile(userId),
fetchPreferences(userId)
]);
return { profile, preferences };
}
// React component with lazy loading
function Dashboard({ userId }) {
// Critical data loaded immediately
const { profile, preferences } = useInitialData(userId);
// Notifications: load when header mounts
const notifications = useLazyQuery(
() => fetchNotifications(userId),
{ loadOn: 'mount' }
);
// Analytics: load when tab is selected
const [analyticsTab, setAnalyticsTab] = useState(false);
const analytics = useLazyQuery(
() => fetchAnalytics(userId),
{ loadOn: analyticsTab }
);
// History: load when scrolled into view
const historyRef = useRef();
const history = useLazyQuery(
() => fetchHistory(userId),
{ loadOn: useIntersectionObserver(historyRef) }
);
return (
<div>
<Header profile={profile} notifications={notifications} />
<Tabs>
<Tab label="Overview">...</Tab>
<Tab label="Analytics" onSelect={() => setAnalyticsTab(true)}>
{analytics.loading ? <Skeleton /> : <AnalyticsChart data={analytics.data} />}
</Tab>
</Tabs>
<div ref={historyRef}>
{history.data && <HistoryList items={history.data} />}
</div>
</div>
);
}// Load when user clicks tab
const [showDetails, setShowDetails] = useState(false);
const details = useQuery(fetchDetails, { enabled: showDetails });
<Tab onClick={() => setShowDetails(true)}>
{details.data ?? <Skeleton />}
</Tab>function LazySection({ loadFn, children }) {
const ref = useRef();
const [loaded, setLoaded] = useState(false);
const data = useQuery(loadFn, { enabled: loaded });
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => entry.isIntersecting && setLoaded(true)
);
observer.observe(ref.current);
return () => observer.disconnect();
}, []);
return <div ref={ref}>{data.data ? children(data.data) : <Skeleton />}</div>;
}// Next.js / React Router
const AnalyticsPage = lazy(() => import('./AnalyticsPage'));
<Route path="/analytics" element={
<Suspense fallback={<Loading />}>
<AnalyticsPage />
</Suspense>
} />function OrderList() {
const [page, setPage] = useState(1);
const { data, hasMore } = useOrders({ page, limit: 20 });
return (
<>
{data.map(order => <OrderRow key={order.id} order={order} />)}
{hasMore && <button onClick={() => setPage(p => p + 1)}>Load More</button>}
</>
);
}Promise.all| Eager (Usually Bad) | Lazy (Usually Good) |
|---|---|
| Load all on mount | Load visible content first |
| Fetch hidden tab data | Fetch when tab selected |
| Full list at once | Paginate / infinite scroll |
| Below-fold content | Intersection observer |
| Excuse | Reality |
|---|---|
| "Might need it" | Load when you do need it. |
| "Parallel is faster" | Not loading is fastest. |
| "Simpler" | Slow isn't simple. |
| "It's not much data" | It adds up. Bandwidth costs. |
| "Better UX to have it ready" | Slow load is worse UX. |