Skip to content

Commit

Permalink
fix: add logic for force reload when failed to update application bec…
Browse files Browse the repository at this point in the history
…ause of browser or cdn cache, update status on click
zardoy committed Jan 8, 2024
1 parent 49e8e27 commit aecd64c
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/globalState.ts
Original file line number Diff line number Diff line change
@@ -187,6 +187,9 @@ export const showNotification = (newNotification: Partial<typeof notification>)
// todo restore auto-save on interval for player data! (or implement it in flying squid since there is already auto-save for world)

window.addEventListener('unload', (e) => {
if (!window.justReloaded) {
sessionStorage.justReloaded = false
}
void saveServer()
})

@@ -201,6 +204,10 @@ window.inspectPlayer = () => require('fs').promises.readFile('/world/playerdata/

// todo move from global state
window.addEventListener('beforeunload', (event) => {
if (!window.justReloaded) {
sessionStorage.justReloaded = false
}

// todo-low maybe exclude chat?
if (!isGameActive(true) && activeModalStack.at(-1)?.elem?.id !== 'chat') return
if (sessionStorage.lastReload && !options.preventDevReloadWhilePlaying) return
15 changes: 13 additions & 2 deletions src/react/MainMenu.tsx
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ interface Props {
const refreshApp = async () => {
const registration = await navigator.serviceWorker.getRegistration()
await registration?.unregister()
window.justReloaded = true
sessionStorage.justReloaded = true
window.location.reload()
}

@@ -36,7 +38,13 @@ export default ({ connectToServerAction, mapsProvider, singleplayerAction, optio
fetch('./version.txt').then(async (f) => {
if (f.status === 404) return
const contents = await f.text()
setVersionStatus(`(${contents === process.env.BUILD_VERSION ? 'latest' : 'new version available'})`)
const isLatest = contents === process.env.BUILD_VERSION
if (!isLatest && sessionStorage.justReloaded) {
// try to force bypass cache
location.search = '?update=true'
}
sessionStorage.justReloaded = false
setVersionStatus(`(${isLatest ? 'latest' : 'new version available'})`)
setVersionTitle(`Loaded: ${process.env.BUILD_VERSION}. Remote: ${contents}`)
}, () => { })
}
@@ -114,7 +122,10 @@ export default ({ connectToServerAction, mapsProvider, singleplayerAction, optio
<div className={styles['bottom-info']}>
<span
title={`${versionTitle} (click to reload)`}
onClick={refreshApp}
onClick={async () => {
setVersionStatus('(reloading)')
await refreshApp()
}}
className={styles['product-info']}
>
Prismarine Web Client {versionStatus}

0 comments on commit aecd64c

Please sign in to comment.