Merge pull request #4223 from overleaf/bg-load-server-worker-timeout

add timeout on loading of service worker

GitOrigin-RevId: d2153c9abf1dbd6eee3b2fcfd770a21766ebb84e
This commit is contained in:
Timothée Alby 2021-06-21 16:00:51 +02:00 committed by Copybot
parent 392b854f49
commit 866e2f0aab
2 changed files with 11 additions and 3 deletions

View file

@ -369,7 +369,7 @@ If the project has been renamed please look in your project list for a new proje
if (getMeta('ol-resetServiceWorker')) {
unregisterServiceWorker()
} else if (getMeta('ol-enablePdfCaching')) {
loadServiceWorker()
loadServiceWorker({ timeout: 5000 })
}
export default angular.bootstrap(document.body, ['SharelatexApp'])

View file

@ -11,9 +11,9 @@ export function waitForServiceWorker() {
return pendingWorkerSetup
}
export function loadServiceWorker() {
export function loadServiceWorker(options) {
if (supportsServiceWorker()) {
pendingWorkerSetup = navigator.serviceWorker
const workerSetup = navigator.serviceWorker
.register('/serviceWorker.js', {
scope: '/project/',
})
@ -38,6 +38,14 @@ export function loadServiceWorker() {
.catch(error =>
captureException(OError.tag(error, 'Cannot register serviceWorker'))
)
if (options && options.timeout > 0) {
const workerTimeout = new Promise(resolve => {
setTimeout(resolve, options.timeout)
})
pendingWorkerSetup = Promise.race([workerSetup, workerTimeout])
} else {
pendingWorkerSetup = workerSetup
}
}
}