Skip to content

Commit

Permalink
Merge pull request #1135 from geyserfund/hot-fix/geyser-reload-safari
Browse files Browse the repository at this point in the history
Hot-fix/geyser-reload-safari
  • Loading branch information
sajald77 authored Sep 1, 2023
2 parents b46e6c6 + a8fa912 commit b387f4e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/config/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import React, { useMemo } from 'react'
import { Navigate, Route, Routes } from 'react-router-dom'

import { __production__, getPath, PathName } from '../constants'
import { handleAssetLoadError } from '../helpers'
import { FailedAuth, TwitterSuccess } from '../pages/auth'
import BadgesPage from '../pages/badges/BadgesPage'
import NotAuthorized from '../pages/notAuthorized'
import NotFoundPage from '../pages/notFound'
import { PrivateRoute } from './PrivateRoute'

const handleCatch = () => {
window.location.reload()
return {} as any
}

// GRANTS

const Grants = import('../pages/grants').catch(handleCatch)
const Grants = import('../pages/grants').catch(handleAssetLoadError)

const GrantsLandingPage = loadable(() =>
Grants.then((m) => m.GrantsLandingPage),
Expand All @@ -28,7 +24,9 @@ const GrantPage = loadable(() => Grants.then((m) => m.GrantPage))

// PROJECT LAUNCH

const ProjectLaunch = import('../pages/projectCreate').catch(handleCatch)
const ProjectLaunch = import('../pages/projectCreate').catch(
handleAssetLoadError,
)

const ProjectCreateStart = loadable(() =>
ProjectLaunch.then((m) => m.ProjectCreateStart),
Expand All @@ -46,15 +44,17 @@ const ProjectCreate = loadable(() => ProjectLaunch.then((m) => m.ProjectCreate))

// ENTRY VIEW & EDIT

const Entry = import('../pages/entry').catch(handleCatch)
const Entry = import('../pages/entry').catch(handleAssetLoadError)

const EntryCreateEdit = loadable(() => Entry.then((m) => m.EntryCreateEdit))
const EntryPreview = loadable(() => Entry.then((m) => m.EntryPreview))
const EntryPage = loadable(() => Entry.then((m) => m.EntryPage))

// PROJECT DASHBOARD

const CreatorDashboard = import('../pages/projectDashboard').catch(handleCatch)
const CreatorDashboard = import('../pages/projectDashboard').catch(
handleAssetLoadError,
)

const ProjectDashboardPage = loadable(() =>
CreatorDashboard.then((m) => m.ProjectDashboardPage),
Expand Down Expand Up @@ -82,16 +82,16 @@ const ProjectSettings = loadable(() =>
)

const ProjectView = loadable(() =>
import('../pages/projectView').catch(handleCatch),
import('../pages/projectView').catch(handleAssetLoadError),
)

const Profile = loadable(() =>
import('../pages/profile/Profile').catch(handleCatch),
import('../pages/profile/Profile').catch(handleAssetLoadError),
)

// LANDING PAGE

const Landing = import('../pages/landing').catch(handleCatch)
const Landing = import('../pages/landing').catch(handleAssetLoadError)

const MobileLeaderboard = loadable(() =>
Landing.then((m) => m.MobileLeaderboard),
Expand All @@ -105,7 +105,7 @@ const LandingFeed = loadable(() => Landing.then((m) => m.LandingFeed))
// ABOUT PAGE

const AboutPage = loadable(() =>
import('../pages/about/About').catch(handleCatch),
import('../pages/about/About').catch(handleAssetLoadError),
)

type PlatformRoutes = {
Expand Down
2 changes: 1 addition & 1 deletion src/context/btc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const BtcProvider = ({ children }: { children: React.ReactNode }) => {

const now = DateTime.local().toMillis()

if (now - timeLineMilis > A_MINUTE_IN_MILIS) {
if (now - timeLineMilis < A_MINUTE_IN_MILIS) {
isOld = false
}

Expand Down
37 changes: 37 additions & 0 deletions src/helpers/handleAssetLoadError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DateTime } from 'luxon'

const CHUNK_LOAD_ERROR = 'ChunkLoadError'
const LOCAL_STORAGE_LAST_REFRESH_KEY = 'ChunkLoadError'
const ONE_MINUTE_IN_MILIS = 60 * 1000

export const handleAssetLoadError = (e: any) => {
if (e?.name && e.name === CHUNK_LOAD_ERROR) {
const refreshed = getRefreshStateFromLocalStorage()
if (!refreshed) {
storeRateToLocalStorage()
window.location.reload()
}
}

return {} as any
}

const getRefreshStateFromLocalStorage = () => {
const values = localStorage.getItem(LOCAL_STORAGE_LAST_REFRESH_KEY)
const timeLineMilis = values ? Number(values) : 0
const now = DateTime.local().toMillis()

let refreshed = false

if (timeLineMilis && now - timeLineMilis < ONE_MINUTE_IN_MILIS) {
refreshed = true
}

return refreshed
}

const storeRateToLocalStorage = () => {
const newDate = DateTime.local().toMillis()

localStorage.setItem(LOCAL_STORAGE_LAST_REFRESH_KEY, `${newDate}`)
}
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './computeBadges'
export * from './fundingCalculation'
export * from './getAvatarMetadata'
export * from './handleAssetLoadError'
export * from './ScrollInvoke'
export * from './useBTCConverter'

0 comments on commit b387f4e

Please sign in to comment.