Skip to content

Commit

Permalink
fix: error in non-pwa apps [LIBS-315] (#789)
Browse files Browse the repository at this point in the history
* fix: error in non-pwa apps

* chore: console log and todo
  • Loading branch information
KaiVandivier committed Mar 6, 2023
1 parent 6262058 commit 590530e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
14 changes: 9 additions & 5 deletions adapter/src/components/ServerVersionProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export const ServerVersionProvider = ({
error: undefined,
baseUrl: url,
})
const [offlineInterfaceLoading, setOfflineInterfaceLoading] = useState(true)
// Skip this loading step in non-pwa apps
const [offlineInterfaceLoading, setOfflineInterfaceLoading] =
useState(pwaEnabled)
const { systemInfo } = systemInfoState
const { baseUrl } = baseUrlState

Expand Down Expand Up @@ -123,10 +125,12 @@ export const ServerVersionProvider = ({
}, [appName, baseUrl])

useEffect(() => {
offlineInterface.ready.then(() => {
setOfflineInterfaceLoading(false)
})
}, [offlineInterface])
if (pwaEnabled) {
offlineInterface.ready.then(() => {
setOfflineInterfaceLoading(false)
})
}
}, [offlineInterface, pwaEnabled])

// This needs to come before 'loading' case to show modal at correct times
if (systemInfoState.error || baseUrlState.error) {
Expand Down
50 changes: 32 additions & 18 deletions pwa/src/offline-interface/offline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,43 @@ export class OfflineInterface {
}
navigator.serviceWorker.addEventListener('message', handleSWMessage)

// (todo: refactor to another function)
// When this promise resolves, it indicates that a connection status
// value has been received from the service worker and is available
// as a property on this offlineInterface.
// Expected to be used by ServerVersionProvider in the app adapter
// to delay rendering the app-runtime Provider until ready.
this.ready = new Promise((resolve) => {
// Listen to status updates and store the latest value here so the
// connection status hook can initialize to this value
this.offlineEvents.on(
swMsgs.dhis2ConnectionStatusUpdate,
({ isConnected }) => {
// If this is the first time receiving an update from the
// SW, resolve the this.ready promise
const shouldResolveReady = this.latestIsConnected === null
this.latestIsConnected = isConnected
if (shouldResolveReady) {
resolve()
}
}
)
})
// Prompt the SW to send back connection status without its usual delay
swMessage(swMsgs.getImmediateDhis2ConnectionStatusUpdate)
this.ready = !this.pwaEnabled
? Promise.resolve()
: new Promise((resolve) => {
// Listen to status updates and store the latest value here so the
// connection status hook can initialize to this value
this.offlineEvents.on(
swMsgs.dhis2ConnectionStatusUpdate,
({ isConnected }) => {
// If this is the first time receiving an update from the
// SW, resolve the this.ready promise
const shouldResolveReady =
this.latestIsConnected === null
this.latestIsConnected = isConnected
if (shouldResolveReady) {
resolve()
}
}
)

try {
// Prompt the SW to send back connection status
// without its usual delay
swMessage(swMsgs.getImmediateDhis2ConnectionStatusUpdate)
} catch {
// It's likely the SW hasn't installed yet, so go ahead and
// resolve `ready` -- the app must be online to get to this case
// anyway
this.latestIsConnected = true
resolve()
}
})
}

/** Basically `checkForUpdates` from registration.js exposed here */
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2225,10 +2225,10 @@
react-docgen "^6.0.0-alpha.0"
url-join "^4.0.1"

"@dhis2/d2-i18n@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@dhis2/d2-i18n/-/d2-i18n-1.1.0.tgz#ec777c5091f747e4c5aa4f9801c62ba4d1ef3d16"
integrity sha512-x3u58goDQsMfBzy50koxNrJjofJTtjRZOfz6f6Py/wMMJfp/T6vZjWMQgcfWH0JrV6d04K1RTt6bI05wqsVQvg==
"@dhis2/d2-i18n@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@dhis2/d2-i18n/-/d2-i18n-1.1.1.tgz#acaca32cd00b60fd6b6f1dee571f2817a50e243c"
integrity sha512-X0jOCIKPaYv/2z0/sdkEvcbRiYu5o1FrOwvitiS6aKFxSL/GJ872I+UdHwpWJtL+yM7Z8E1epljazW0LnHUz0Q==
dependencies:
i18next "^10.3"
moment "^2.24.0"
Expand Down

0 comments on commit 590530e

Please sign in to comment.