From 17142a778a07fa26e16a274298b33a2a67ed2d31 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 25 Oct 2017 14:38:02 -0700 Subject: [PATCH] fix(service-worker): don't block initialization on registration (#19936) Importing ServiceWorkerModule.register() will schedule registration of the Service Worker inside an APP_INITIALIZER. Previously, the Promise returned by navigator.serviceWorker.register() was returned from the initializer function. This has the unwanted side effect of blocking initialization until the SW is registered. Even worse, if the SW script fails to load, this can cause the app initialization to fail. The solution is to not return the registration promise from the initializer function, essentially decoupling registration from the rest of the initialization flow. This change is not unit testable as there are no mocks/adapters yet for navigator.serviceWorker. A future integration test should cover this case with better fidelity. PR Close #19936 --- packages/service-worker/src/module.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/service-worker/src/module.ts b/packages/service-worker/src/module.ts index b9ca82328e..7cdf55e7e2 100644 --- a/packages/service-worker/src/module.ts +++ b/packages/service-worker/src/module.ts @@ -30,8 +30,10 @@ export function ngswAppInitializer( op_filter.call(app.isStable, (stable: boolean) => !!stable) as Observable; const isStable = op_take.call(onStable, 1) as Observable; const whenStable = op_toPromise.call(isStable) as Promise; - return whenStable.then(() => navigator.serviceWorker.register(script, options)) - .then(() => undefined) as Promise; + + // Don't return the Promise, as that will block the application until the SW is registered, and + // cause a crash if the SW registration fails. + whenStable.then(() => navigator.serviceWorker.register(script, options)); }; return initializer; }