fix(service-worker): avoid uncaught rejection warning when registration fails (#30876)

Service worker API `navigator.serviceWorker.register` can fail in multiple ways.
For example, in Chrome, with an unstable network connection you can have the
following error: `An unknown error occurred when fetching the script.`

In the current state, it creates an `Uncaught (in promise) TypeError:` style of
error, which cannot be caught by the user on his own.

I think it's better to log the error over raising an error that cannot be
caught.

PR Close #30876
This commit is contained in:
Hoel IRIS
2019-06-04 16:54:24 +02:00
committed by Misko Hevery
parent 297222f892
commit 81c2a94310
2 changed files with 16 additions and 4 deletions

View File

@ -115,10 +115,11 @@ export function ngswAppInitializer(
}
}
// Don't return anything to avoid blocking the application until the SW is registered or
// causing a crash if the SW registration fails.
// Don't return anything to avoid blocking the application until the SW is registered.
// Catch and log the error if SW registration fails to avoid uncaught rejection warning.
readyToRegister$.pipe(take(1)).subscribe(
() => navigator.serviceWorker.register(script, {scope: options.scope}));
() => navigator.serviceWorker.register(script, {scope: options.scope})
.catch(err => console.error('Service worker registration failed with:', err)));
};
return initializer;
}