Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pwa): update gracefully if missing clients info #679

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions adapter/src/components/PWAUpdateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ function ConfirmReloadModal({ clientsCount, onCancel, onConfirm }) {
<ModalContent>
{i18n.t(
"Updating will reload all {{n}} open instances of this app, and any unsaved data will be lost. Save any data you need to, then click 'Reload' when ready.",
{ n: clientsCount }
{
// 'the' backup makes a coherent sentence if clientsCount is unavailable
n: clientsCount || 'the',
mediremi marked this conversation as resolved.
Show resolved Hide resolved
}
)}
</ModalContent>
<ModalActions>
Expand Down Expand Up @@ -47,7 +50,7 @@ ConfirmReloadModal.propTypes = {
*/
export default function PWAUpdateManager({ offlineInterface }) {
const [confirmReloadModalOpen, setConfirmReloadModalOpen] = useState(false)
const [clientsCountState, setClientsCountState] = useState(1)
const [clientsCountState, setClientsCountState] = useState(null)
const { show } = useAlert(
i18n.t("There's an update available for this app."),
({ onConfirm }) => ({
Expand All @@ -59,16 +62,25 @@ export default function PWAUpdateManager({ offlineInterface }) {
})
)

const confirmReload = async () => {
const { clientsCount } = await offlineInterface.getClientsInfo()
setClientsCountState(clientsCount)
if (clientsCount <= 1) {
// Just one client; go ahead and reload
offlineInterface.useNewSW()
} else {
// Multiple clients open; warn about data loss before reloading
setConfirmReloadModalOpen(true)
}
const confirmReload = () => {
offlineInterface
.getClientsInfo()
.then(({ clientsCount }) => {
setClientsCountState(clientsCount)
if (clientsCount === 1) {
// Just one client; go ahead and reload
offlineInterface.useNewSW()
} else {
// Multiple clients; warn about data loss before reloading
setConfirmReloadModalOpen(true)
}
})
.catch(reason => {
// Didn't get clients info
console.warn(reason)
// Go ahead with confirmation modal with `null` as clientsCount
setConfirmReloadModalOpen(true)
})
}

useEffect(() => {
Expand Down
10 changes: 5 additions & 5 deletions pwa/src/offline-interface/offline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export class OfflineInterface {
}

/**
* Requests clients info from the active service worker. Works for
* both first activation and SW update by using `reg.active` worker.
* Requests clients info from the active service worker.
* @returns {Promise}
*/
getClientsInfo() {
Expand All @@ -75,15 +74,16 @@ export class OfflineInterface {
if (!registration || !registration.active) {
reject('There is no active service worker')
}
// Send request message to SW
registration.active.postMessage({ type: swMsgs.getClientsInfo })
// Send request message to newest SW
const newestSW = registration.waiting || registration.active
newestSW.postMessage({ type: swMsgs.getClientsInfo })
// Resolve with payload received from SW `clientsInfo` message
this.offlineEvents.once(swMsgs.clientsInfo, resolve)
// Clean up potentially unused listeners eventually
setTimeout(() => {
reject('Request for clients info timed out')
this.offlineEvents.removeAllListeners(swMsgs.clientsInfo)
}, 10000)
}, 2000)
})
})
}
Expand Down