fix: eagerly load map caches in browser and use Suspense pattern
Fix race condition where WORLD_MAP/USA_MAP Proxies were accessed before map data finished loading in the browser. The previous implementation only loaded the map sources but didn't populate the MapData caches. Changes: - Browser now eagerly loads both map sources AND populates both caches - Store loading promise and throw it from getMapDataSync() if not ready - This triggers React Suspense to show loading state instead of crashing - Fixes production error: "world map not yet loaded" in browser console 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
4866f699d1
commit
db6be73a1c
|
|
@ -46,10 +46,17 @@ async function ensureMapSourcesLoaded(): Promise<void> {
|
|||
* In browser context, load maps immediately at module initialization
|
||||
* This allows synchronous access in client components
|
||||
*/
|
||||
let browserMapsLoadingPromise: Promise<void> | null = null
|
||||
if (typeof window !== 'undefined') {
|
||||
// Browser: Start loading immediately
|
||||
ensureMapSourcesLoaded().catch(err => {
|
||||
// Browser: Start loading immediately and cache the promise
|
||||
browserMapsLoadingPromise = (async () => {
|
||||
await ensureMapSourcesLoaded()
|
||||
// Populate the caches eagerly
|
||||
await getWorldMapData()
|
||||
await getUSAMapData()
|
||||
})().catch(err => {
|
||||
console.error('[Maps] Failed to load map data in browser:', err)
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -363,13 +370,19 @@ async function getUSAMapData(): Promise<MapData> {
|
|||
|
||||
/**
|
||||
* Get map data synchronously (for client components)
|
||||
* Throws if maps aren't loaded yet
|
||||
* In browser, throws a promise to trigger React Suspense if not loaded yet
|
||||
*/
|
||||
function getMapDataSync(mapId: 'world' | 'usa'): MapData {
|
||||
const cache = mapId === 'world' ? worldMapDataCache : usaMapDataCache
|
||||
|
||||
if (!cache) {
|
||||
// In browser, if maps are still loading, throw the promise to trigger Suspense
|
||||
if (typeof window !== 'undefined' && browserMapsLoadingPromise) {
|
||||
throw browserMapsLoadingPromise
|
||||
}
|
||||
throw new Error(`[Maps] ${mapId} map not yet loaded. Use await getMapData() or ensure maps are preloaded.`)
|
||||
}
|
||||
|
||||
return cache
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue