4.1 KiB
Performance Fixes Applied - January 11, 2026
Issues Identified
1. Infinite Loop in CartContext (CRITICAL)
Problem: The frontend was making hundreds of /api/cart requests per second, causing severe performance degradation.
Root Cause: In frontend/src/context/CartContext.js, the useEffect hook had fetchCart in its dependency array, but fetchCart was recreated on every render, causing an infinite loop.
Solution:
- Wrapped
fetchCartfunction withuseCallbackhook to memoize it with proper dependencies - This ensures the function is only recreated when
isAuthenticatedortokenchanges
2. Missing Product Images in Cart Query
Problem: The backend cart endpoint wasn't preloading product images, causing N+1 query issues.
Solution:
- Updated
/api/cartendpoint inbackend/server.pyto useselectinload(CartItem.product).selectinload(Product.images) - This loads all product images in a single query instead of making separate queries for each product
3. Deprecated FastAPI Event Handlers
Problem: Backend was using deprecated @app.on_event("startup") and @app.on_event("shutdown") decorators, causing deprecation warnings.
Solution:
- Migrated to modern lifespan context manager pattern
- Created
@asynccontextmanagerfunction that handles startup and shutdown - Passed lifespan to FastAPI app initialization
Files Modified
Frontend
frontend/src/context/CartContext.js- Added
useCallbackimport - Wrapped
fetchCartwithuseCallbackhook - Simplified
useEffectdependencies
- Added
Backend
backend/server.py- Added
asynccontextmanagerimport - Created
lifespanfunction for app lifecycle management - Updated cart query to preload product images
- Removed deprecated
@app.on_eventdecorators - Moved logging configuration before app creation
- Added
Performance Improvements
Before Fixes
- Cart Requests: 100+ requests per second (infinite loop)
- Backend Logs: Flooded with cart requests
- Page Load Time: Extremely slow, site unresponsive
- Database Queries: N+1 issues with product images
After Fixes
- Cart Requests: 1-2 requests on page load/navigation (expected behavior)
- Backend Logs: Clean, only showing necessary requests
- Page Load Time: Fast and responsive
- Database Queries: Optimized with eager loading
Testing Performed
- ✅ Frontend build completed successfully
- ✅ Backend server starts without deprecation warnings
- ✅ Cart requests reduced from hundreds to 1-2 per page load
- ✅ Both servers running and communicating properly
- ✅ No infinite loops detected in monitoring
Additional Optimizations Applied
- Product images are now preloaded in all product queries using
selectinload(Product.images) - Cart endpoint now loads product images in a single query
- FastAPI app uses modern lifespan events for better async handling
Verification Steps
To verify the fixes are working:
-
Check Backend Logs:
tail -f /proc/$(pgrep -f "python server.py")/fd/1Should see only occasional cart requests, not continuous spam
-
Monitor Cart Requests:
watch -n 1 'tail -50 /proc/$(pgrep -f "python server.py")/fd/1 | grep "/api/cart" | wc -l'Count should remain low (0-2), not increasing rapidly
-
Frontend Console:
- Open browser DevTools → Network tab
- Filter by "cart"
- Should see only 1-2 requests when navigating pages
Recommended Next Steps
- Production Build: Test with production build for maximum performance
- Caching: Consider adding Redis caching for frequently accessed data
- Database Indexing: Verify all foreign keys and frequently queried columns have indexes
- Load Testing: Perform load testing to ensure system handles concurrent users