CDN Fallbacks and Error Handling
MapLibreum now includes robust CDN fallback mechanisms and error handling to ensure maps display properly even when external resources are blocked or unavailable.
Problem Addressed
Previously, MapLibre maps depended on external CDN resources from unpkg.com. When these CDNs were blocked (common in corporate environments, content blockers, or network restrictions), maps would fail completely, showing blank pages with no indication of what went wrong.
Solution Implemented
Multi-CDN Fallbacks
Maps now attempt to load MapLibre GL JS from multiple CDNs in sequence:
Primary: unpkg.com (fastest, most reliable)
Secondary: cdn.jsdelivr.net (jsDelivr CDN)
Tertiary: cdnjs.cloudflare.com (Cloudflare CDN)
Graceful Error Handling
When all CDNs fail to load, maps now display a user-friendly error message instead of a blank page:
🗺️
Map Loading Error
Failed to load MapLibre GL JavaScript library
This may be due to network restrictions or CDN availability issues.
Try refreshing the page or check your internet connection.
CSS Fallbacks
CSS resources also include fallback mechanisms to ensure styling works across different CDN availability scenarios.
Technical Implementation
The solution includes:
Asynchronous CDN Loading: Progressive fallback through multiple CDN providers
Error Detection: Automatic detection of CDN loading failures
Styled Error Messages: Professional, informative error displays
Graceful Degradation: Clear communication when technical issues occur
Try-Catch Protection: Robust error handling around map initialization
Benefits
✅ Improved Reliability: Maps work even when primary CDN is blocked
✅ Better UX: Clear error messages instead of confusing blank pages
✅ Professional Appearance: Styled error states maintain visual consistency
✅ Debugging Aid: Console logging helps identify specific CDN issues
✅ Deployment Ready: Works reliably in various hosting environments
Backward Compatibility
This enhancement is fully backward compatible. Existing code continues to work unchanged, with the added benefit of improved reliability and error handling.
Testing
Comprehensive tests ensure the fallback mechanisms work correctly:
pytest tests/test_cdn_fallbacks.py -v
The test suite validates:
Multi-CDN fallback implementation
Error handling functionality
CSS fallback mechanisms
Map initialization error handling
Styled error message display
Preservation of normal map functionality