Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: Retry map download if it fails

Blue Mouse67c962ab7b34
diff --git a/web/app/pages/Project/View/components/InteractiveMap.tsx b/web/app/pages/Project/View/components/InteractiveMap.tsx
index c97a31b54..1e4af8969 100644
--- a/web/app/pages/Project/View/components/InteractiveMap.tsx
+++ b/web/app/pages/Project/View/components/InteractiveMap.tsx
@@ -64,6 +64,9 @@ const InteractiveMapCore = ({
const [regionsGeoData, setRegionsGeoData] = useState<GeoJsonObject | null>(
null,
)
+ const [isCountriesGeoDataLoading, setIsCountriesGeoDataLoading] =
+ useState(true)
+ const [isRegionsGeoDataLoading, setIsRegionsGeoDataLoading] = useState(false)
const [filteredRegionsGeoData, setFilteredRegionsGeoData] =
useState<GeoJsonObject | null>(null)
const [mapView, setMapView] = useState<'countries' | 'regions'>('countries')
@@ -84,26 +87,75 @@ const InteractiveMapCore = ({
const isErrorsTab = activeTab === PROJECT_TABS.errors
const isPerformanceTab = activeTab === PROJECT_TABS.performance
const isGeoDataLoading =
- mapView === 'regions' ? !regionsGeoData : !countriesGeoData
+ mapView === 'regions'
+ ? isRegionsGeoDataLoading && !regionsGeoData
+ : isCountriesGeoDataLoading && !countriesGeoData
useEffect(() => {
- const loadGeoData = async () => {
+ let isCancelled = false
+
+ const loadCountries = async () => {
+ setIsCountriesGeoDataLoading(true)
+
try {
- const [countries, regions] = await Promise.all([
- loadCountriesGeoData(),
- isSelfhosted ? Promise.resolve(null) : loadRegionsGeoData(),
- ])
- setCountriesGeoData(countries)
- setRegionsGeoData(regions)
- setFilteredRegionsGeoData(regions)
+ const countries = await loadCountriesGeoData()
+
+ if (!isCancelled) {
+ setCountriesGeoData(countries)
+ }
} catch (reason) {
- console.error('Failed to load geographic data:', reason)
+ if (!isCancelled) {
+ console.error('Failed to load countries geographic data:', reason)
+ }
+ } finally {
+ if (!isCancelled) {
+ setIsCountriesGeoDataLoading(false)
+ }
}
}
- loadGeoData()
+ loadCountries()
+
+ return () => {
+ isCancelled = true
+ }
}, [])
+ useEffect(() => {
+ if (isSelfhosted || mapView !== 'regions' || regionsGeoData) {
+ return
+ }
+
+ let isCancelled = false
+
+ const loadRegions = async () => {
+ setIsRegionsGeoDataLoading(true)
+
+ try {
+ const regions = await loadRegionsGeoData()
+
+ if (!isCancelled) {
+ setRegionsGeoData(regions)
+ setFilteredRegionsGeoData(regions)
+ }
+ } catch (reason) {
+ if (!isCancelled) {
+ console.error('Failed to load regions geographic data:', reason)
+ }
+ } finally {
+ if (!isCancelled) {
+ setIsRegionsGeoDataLoading(false)
+ }
+ }
+ }
+
+ loadRegions()
+
+ return () => {
+ isCancelled = true
+ }
+ }, [mapView, regionsGeoData])
+
useEffect(() => {
setGeoJsonKey((v) => v + 1)
}, [data, regionData, mapView])
diff --git a/web/app/utils/geoData.ts b/web/app/utils/geoData.ts
index c5130235d..caee5dea8 100644
--- a/web/app/utils/geoData.ts
+++ b/web/app/utils/geoData.ts
@@ -4,14 +4,54 @@ let countriesGeoData: GeoJsonObject | null = null
let regionsGeoData: GeoJsonObject | null = null
+const GEO_DATA_RETRY_DELAYS_MS = [250, 750]
+
+const sleep = (ms: number) =>
+ new Promise((resolve) => {
+ setTimeout(resolve, ms)
+ })
+
+const fetchGeoData = async (url: string): Promise<GeoJsonObject> => {
+ let lastError: unknown
+
+ for (
+ let attempt = 0;
+ attempt <= GEO_DATA_RETRY_DELAYS_MS.length;
+ attempt += 1
+ ) {
+ try {
+ const response = await fetch(url, {
+ cache: attempt === 0 ? 'force-cache' : 'reload',
+ })
+
+ if (!response.ok) {
+ throw new Error(
+ `Failed to load "${url}": ${response.status} ${response.statusText}`,
+ )
+ }
+
+ return (await response.json()) as GeoJsonObject
+ } catch (reason) {
+ lastError = reason
+
+ if (attempt === GEO_DATA_RETRY_DELAYS_MS.length) {
+ break
+ }
+
+ await sleep(GEO_DATA_RETRY_DELAYS_MS[attempt])
+ }
+ }
+
+ throw lastError
+}
+
export const loadCountriesGeoData = async (): Promise<GeoJsonObject> => {
if (countriesGeoData) {
return countriesGeoData
}
try {
- const response = await fetch('/geo/countries_50m.geojson')
- countriesGeoData = await response.json()
+ countriesGeoData = await fetchGeoData('/geo/countries_50m.geojson')
return countriesGeoData as GeoJsonObject
} catch (reason) {
console.error('Failed to load countries geo data:', reason)
@@ -25,8 +65,7 @@ export const loadRegionsGeoData = async (): Promise<GeoJsonObject> => {
}
try {
- const response = await fetch('/geo/regions.geojson')
- regionsGeoData = await response.json()
+ regionsGeoData = await fetchGeoData('/geo/regions.geojson')
return regionsGeoData as GeoJsonObject
} catch (reason) {
console.error('Failed to load regions geo data:', reason)