2017-09-28 19:18:12 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-11-11 18:39:27 -05:00
|
|
|
import {isPlatformBrowser} from '@angular/common';
|
2018-02-27 17:06:06 -05:00
|
|
|
import {APP_INITIALIZER, ApplicationRef, InjectionToken, Injector, ModuleWithProviders, NgModule, PLATFORM_ID} from '@angular/core';
|
2018-11-07 15:45:40 -05:00
|
|
|
import {Observable, of } from 'rxjs';
|
|
|
|
import {delay, filter, take} from 'rxjs/operators';
|
2017-09-28 19:18:12 -04:00
|
|
|
|
|
|
|
import {NgswCommChannel} from './low_level';
|
|
|
|
import {SwPush} from './push';
|
|
|
|
import {SwUpdate} from './update';
|
|
|
|
|
2019-04-25 09:51:07 -04:00
|
|
|
/**
|
|
|
|
* Token that can be used to provide options for `ServiceWorkerModule` outside of
|
|
|
|
* `ServiceWorkerModule.register()`.
|
|
|
|
*
|
2019-04-25 09:51:08 -04:00
|
|
|
* You can use this token to define a provider that generates the registration options at runtime,
|
|
|
|
* for example via a function call:
|
|
|
|
*
|
|
|
|
* {@example service-worker/registration-options/module.ts region="registration-options"
|
|
|
|
* header="app.module.ts" linenums="false"}
|
|
|
|
*
|
2019-04-25 09:51:07 -04:00
|
|
|
* @publicApi
|
|
|
|
*/
|
2019-04-25 09:51:07 -04:00
|
|
|
export abstract class SwRegistrationOptions {
|
2019-04-25 09:51:08 -04:00
|
|
|
/**
|
|
|
|
* Whether the ServiceWorker will be registered and the related services (such as `SwPush` and
|
|
|
|
* `SwUpdate`) will attempt to communicate and interact with it.
|
|
|
|
*
|
|
|
|
* Default: true
|
|
|
|
*/
|
2017-11-30 16:16:37 -05:00
|
|
|
enabled?: boolean;
|
2019-04-25 09:51:08 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A URL that defines the ServiceWorker's registration scope; that is, what range of URLs it can
|
|
|
|
* control. It will be used when calling
|
|
|
|
* [ServiceWorkerContainer#register()](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
|
|
|
|
*/
|
|
|
|
scope?: string;
|
2018-01-28 11:38:16 -05:00
|
|
|
|
2018-11-07 15:45:40 -05:00
|
|
|
/**
|
|
|
|
* Defines the ServiceWorker registration strategy, which determines when it will be registered
|
|
|
|
* with the browser.
|
|
|
|
*
|
|
|
|
* The default behavior of registering once the application stabilizes (i.e. as soon as there are
|
|
|
|
* no pending micro- and macro-tasks), is designed register the ServiceWorker as soon as possible
|
|
|
|
* but without affecting the application's first time load.
|
|
|
|
*
|
|
|
|
* Still, there might be cases where you want more control over when the ServiceWorker is
|
|
|
|
* registered (e.g. there might be a long-running timeout or polling interval, preventing the app
|
|
|
|
* to stabilize). The available option are:
|
|
|
|
*
|
|
|
|
* - `registerWhenStable`: Register as soon as the application stabilizes (no pending
|
|
|
|
* micro-/macro-tasks).
|
|
|
|
* - `registerImmediately`: Register immediately.
|
|
|
|
* - `registerWithDelay:<timeout>`: Register with a delay of `<timeout>` milliseconds. For
|
|
|
|
* example, use `registerWithDelay:5000` to register the ServiceWorker after 5 seconds. If
|
|
|
|
* `<timeout>` is omitted, is defaults to `0`, which will register the ServiceWorker as soon
|
|
|
|
* as possible but still asynchronously, once all pending micro-tasks are completed.
|
|
|
|
* - An [Observable](guide/observables) factory function: A function that returns an `Observable`.
|
|
|
|
* The function will be used at runtime to obtain and subscribe to the `Observable` and the
|
|
|
|
* ServiceWorker will be registered as soon as the first value is emitted.
|
|
|
|
*
|
|
|
|
* Default: 'registerWhenStable'
|
|
|
|
*/
|
|
|
|
registrationStrategy?: string|(() => Observable<unknown>);
|
2017-11-30 16:16:37 -05:00
|
|
|
}
|
|
|
|
|
2017-09-28 19:18:12 -04:00
|
|
|
export const SCRIPT = new InjectionToken<string>('NGSW_REGISTER_SCRIPT');
|
|
|
|
|
|
|
|
export function ngswAppInitializer(
|
2019-04-25 09:51:07 -04:00
|
|
|
injector: Injector, script: string, options: SwRegistrationOptions,
|
2017-11-11 18:39:27 -05:00
|
|
|
platformId: string): Function {
|
2017-09-28 19:18:12 -04:00
|
|
|
const initializer = () => {
|
2017-11-11 18:39:27 -05:00
|
|
|
if (!(isPlatformBrowser(platformId) && ('serviceWorker' in navigator) &&
|
|
|
|
options.enabled !== false)) {
|
2017-09-28 19:18:12 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-10-25 17:38:02 -04:00
|
|
|
|
2017-11-29 16:24:20 -05:00
|
|
|
// Wait for service worker controller changes, and fire an INITIALIZE action when a new SW
|
|
|
|
// becomes active. This allows the SW to initialize itself even if there is no application
|
|
|
|
// traffic.
|
|
|
|
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
|
|
|
if (navigator.serviceWorker.controller !== null) {
|
|
|
|
navigator.serviceWorker.controller.postMessage({action: 'INITIALIZE'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-07 15:45:40 -05:00
|
|
|
let readyToRegister$: Observable<unknown>;
|
|
|
|
|
2018-01-28 11:38:16 -05:00
|
|
|
if (typeof options.registrationStrategy === 'function') {
|
2018-11-07 15:45:40 -05:00
|
|
|
readyToRegister$ = options.registrationStrategy();
|
2018-01-28 11:38:16 -05:00
|
|
|
} else {
|
2018-11-07 15:45:40 -05:00
|
|
|
const [strategy, ...args] = (options.registrationStrategy || 'registerWhenStable').split(':');
|
|
|
|
switch (strategy) {
|
|
|
|
case 'registerImmediately':
|
|
|
|
readyToRegister$ = of (null);
|
|
|
|
break;
|
|
|
|
case 'registerWithDelay':
|
|
|
|
readyToRegister$ = of (null).pipe(delay(+args[0] || 0));
|
|
|
|
break;
|
|
|
|
case 'registerWhenStable':
|
|
|
|
const appRef = injector.get<ApplicationRef>(ApplicationRef);
|
|
|
|
readyToRegister$ = appRef.isStable.pipe(filter(stable => stable));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unknown strategy.
|
|
|
|
throw new Error(
|
|
|
|
`Unknown ServiceWorker registration strategy: ${options.registrationStrategy}`);
|
2018-01-28 11:38:16 -05:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 15:45:40 -05:00
|
|
|
|
2019-06-04 10:54:24 -04:00
|
|
|
// 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.
|
2018-11-07 15:45:40 -05:00
|
|
|
readyToRegister$.pipe(take(1)).subscribe(
|
2019-06-04 10:54:24 -04:00
|
|
|
() => navigator.serviceWorker.register(script, {scope: options.scope})
|
|
|
|
.catch(err => console.error('Service worker registration failed with:', err)));
|
2017-09-28 19:18:12 -04:00
|
|
|
};
|
|
|
|
return initializer;
|
|
|
|
}
|
|
|
|
|
2017-11-11 18:39:27 -05:00
|
|
|
export function ngswCommChannelFactory(
|
2019-04-25 09:51:07 -04:00
|
|
|
opts: SwRegistrationOptions, platformId: string): NgswCommChannel {
|
2017-11-11 18:39:27 -05:00
|
|
|
return new NgswCommChannel(
|
2018-05-09 11:11:43 -04:00
|
|
|
isPlatformBrowser(platformId) && opts.enabled !== false ? navigator.serviceWorker :
|
|
|
|
undefined);
|
2017-09-28 19:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-19 07:12:20 -04:00
|
|
|
* @publicApi
|
2017-09-28 19:18:12 -04:00
|
|
|
*/
|
|
|
|
@NgModule({
|
|
|
|
providers: [SwPush, SwUpdate],
|
|
|
|
})
|
|
|
|
export class ServiceWorkerModule {
|
2017-11-30 16:16:37 -05:00
|
|
|
/**
|
|
|
|
* Register the given Angular Service Worker script.
|
|
|
|
*
|
|
|
|
* If `enabled` is set to `false` in the given options, the module will behave as if service
|
|
|
|
* workers are not supported by the browser, and the service worker will not be registered.
|
|
|
|
*/
|
2019-04-25 09:51:07 -04:00
|
|
|
static register(script: string, opts: SwRegistrationOptions = {}):
|
2018-09-14 17:55:16 -04:00
|
|
|
ModuleWithProviders<ServiceWorkerModule> {
|
2017-09-28 19:18:12 -04:00
|
|
|
return {
|
|
|
|
ngModule: ServiceWorkerModule,
|
|
|
|
providers: [
|
|
|
|
{provide: SCRIPT, useValue: script},
|
2019-04-25 09:51:07 -04:00
|
|
|
{provide: SwRegistrationOptions, useValue: opts},
|
2017-11-11 18:39:27 -05:00
|
|
|
{
|
|
|
|
provide: NgswCommChannel,
|
|
|
|
useFactory: ngswCommChannelFactory,
|
2019-04-25 09:51:07 -04:00
|
|
|
deps: [SwRegistrationOptions, PLATFORM_ID]
|
2017-11-11 18:39:27 -05:00
|
|
|
},
|
2017-09-28 19:18:12 -04:00
|
|
|
{
|
|
|
|
provide: APP_INITIALIZER,
|
|
|
|
useFactory: ngswAppInitializer,
|
2019-04-25 09:51:07 -04:00
|
|
|
deps: [Injector, SCRIPT, SwRegistrationOptions, PLATFORM_ID],
|
2017-09-28 19:18:12 -04:00
|
|
|
multi: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2017-11-30 16:16:37 -05:00
|
|
|
}
|