5.5 KiB
Unused JavaScript Optimization Guide
Current Issue
Lighthouse reports 980 KiB of unused JavaScript. This guide helps identify and fix it.
Strategy
1. Bundle Analysis
First, analyze what's taking up space:
cd frontend
npm install # Install source-map-explorer if needed
npm run analyze
This creates bundle-report.html - open it in a browser to see:
- Which packages are largest
- Which files import them
- Unused code within packages
2. Lazy Load Heavy Dependencies
A. Recharts (Charts Library)
Size: ~200+ KiB
Usage: Only in billing, analytics, and scheduler dashboards
Before:
import { LineChart, Line } from 'recharts';
After:
import { LazyLineChart, Line } from '../../utils/lazyRecharts';
import { Suspense } from 'react';
<Suspense fallback={<ChartSkeleton />}>
<LazyLineChart>
<Line />
</LazyLineChart>
</Suspense>
Files to update:
frontend/src/components/billing/UsageTrends.tsxfrontend/src/components/SchedulerDashboard/SchedulerCharts.tsxfrontend/src/components/ContentPlanningDashboard/components/MonitoringCharts.tsxfrontend/src/components/shared/charts/AdvancedChartComponents.tsx
B. Wix SDK
Size: ~100+ KiB
Usage: Only in WixTestPage and WixCallbackPage
Before:
import { createClient } from '@wix/sdk';
After:
const { createClient } = await import('@wix/sdk');
// Or use lazy loading in component
Files to update:
frontend/src/components/WixTestPage/WixTestPage.tsxfrontend/src/components/WixCallbackPage/WixCallbackPage.tsxfrontend/src/components/OnboardingWizard/common/usePlatformConnections.ts
C. Framer Motion (Animations)
Size: ~246 KiB
Usage: Used extensively but can be optimized
Strategy:
- Use CSS animations for simple transitions
- Lazy load framer-motion for non-critical animations
- Use
will-changeCSS property instead of complex animations
Example:
// Instead of complex framer-motion for simple fade
// Use CSS:
const fadeIn = {
animation: 'fadeIn 0.3s ease-in'
};
3. Tree Shaking Optimization
A. Material-UI Icons
Issue: Importing entire icon set
Before:
import { TrendingUp, TrendingDown } from '@mui/icons-material';
After (already optimized, but verify):
// React Scripts should tree-shake automatically
// But verify imports are specific
B. Lucide React Icons
Issue: Large icon library, some can be replaced with MUI icons
Strategy: Replace lucide-react icons with MUI icons where possible
Before:
import { TrendingUp } from 'lucide-react';
After:
import { TrendingUp } from '@mui/icons-material';
4. Remove Unused Dependencies
Check if these are actually used:
@wix/blog- Only in WixTestPagelucide-react- Can be replaced with MUI icons in many placeszod- Verify if all schemas are used
5. Code Splitting Improvements
A. Route-Level Splitting (Already Done ✅)
Routes are already lazy-loaded.
B. Component-Level Splitting
Lazy load heavy components within routes:
// In MainDashboard.tsx
const EnhancedBillingDashboard = lazy(() =>
import('../billing/EnhancedBillingDashboard')
);
6. Dynamic Imports for Heavy Features
A. Charts
Only load charts when dashboard is viewed:
const loadCharts = () => import('recharts');
B. Analytics
Only load analytics when analytics tab is opened:
const loadAnalytics = () => import('./components/AnalyticsInsights');
Implementation Steps
Step 1: Analyze Bundle
npm run analyze
# Open bundle-report.html
Step 2: Identify Large Dependencies
Look for:
- Packages > 50 KiB
- Packages used in < 3 places
- Packages that can be lazy-loaded
Step 3: Lazy Load Heavy Dependencies
- Create lazy wrappers (see
lazyRecharts.tsx) - Update imports to use lazy versions
- Add Suspense boundaries
Step 4: Replace Icons
- Find lucide-react imports
- Replace with MUI icons where possible
- Remove lucide-react if not needed
Step 5: Test
npm run build
npm run analyze # Check if bundle size decreased
Expected Results
Before
- Unused JavaScript: 980 KiB
- Bundle size: Large initial load
After
- Unused JavaScript: < 200 KiB (estimated)
- Bundle size: Reduced by ~500-700 KiB
- Performance: Improved initial load time
Files to Update
High Priority (Large Impact)
- ✅
frontend/src/utils/lazyRecharts.tsx- Created - ✅
frontend/src/utils/lazyWix.ts- Created frontend/src/components/billing/UsageTrends.tsx- Use lazy rechartsfrontend/src/components/SchedulerDashboard/SchedulerCharts.tsx- Use lazy rechartsfrontend/src/components/WixTestPage/WixTestPage.tsx- Use lazy Wix SDK
Medium Priority
frontend/src/components/ContentPlanningDashboard/components/MonitoringCharts.tsxfrontend/src/components/shared/charts/AdvancedChartComponents.tsx- Replace lucide-react with MUI icons in billing components
Low Priority (Optimization)
- Optimize framer-motion usage
- Further code splitting within components
Monitoring
After changes, verify:
- Bundle size decreased
- Lighthouse "Reduce unused JavaScript" improved
- No broken functionality
- Charts still work (with loading states)
Next Steps
- Run
npm run analyzeto see current bundle - Update components to use lazy-loaded dependencies
- Test functionality
- Re-run Lighthouse audit