diff --git a/packages/service-worker/src/module.ts b/packages/service-worker/src/module.ts index 7cdf55e7e2..ed5609cf94 100644 --- a/packages/service-worker/src/module.ts +++ b/packages/service-worker/src/module.ts @@ -16,14 +16,18 @@ import {NgswCommChannel} from './low_level'; import {SwPush} from './push'; import {SwUpdate} from './update'; +export abstract class RegistrationOptions { + scope?: string; + enabled?: boolean; +} + export const SCRIPT = new InjectionToken('NGSW_REGISTER_SCRIPT'); -export const OPTS = new InjectionToken('NGSW_REGISTER_OPTIONS'); export function ngswAppInitializer( injector: Injector, script: string, options: RegistrationOptions): Function { const initializer = () => { const app = injector.get(ApplicationRef); - if (!('serviceWorker' in navigator)) { + if (!('serviceWorker' in navigator) || options.enabled === false) { return; } const onStable = @@ -33,13 +37,13 @@ export function ngswAppInitializer( // 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)); + whenStable.then(() => navigator.serviceWorker.register(script, {scope: options.scope})); }; return initializer; } -export function ngswCommChannelFactory(): NgswCommChannel { - return new NgswCommChannel(navigator.serviceWorker); +export function ngswCommChannelFactory(opts: RegistrationOptions): NgswCommChannel { + return new NgswCommChannel(opts.enabled !== false ? navigator.serviceWorker : undefined); } /** @@ -49,20 +53,27 @@ export function ngswCommChannelFactory(): NgswCommChannel { providers: [SwPush, SwUpdate], }) export class ServiceWorkerModule { - static register(script: string, opts: RegistrationOptions = {}): ModuleWithProviders { + /** + * 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. + */ + static register(script: string, opts: {scope?: string; enabled?: boolean;} = {}): + ModuleWithProviders { return { ngModule: ServiceWorkerModule, providers: [ {provide: SCRIPT, useValue: script}, - {provide: OPTS, useValue: opts}, - {provide: NgswCommChannel, useFactory: ngswCommChannelFactory}, + {provide: RegistrationOptions, useValue: opts}, + {provide: NgswCommChannel, useFactory: ngswCommChannelFactory, deps: [RegistrationOptions]}, { provide: APP_INITIALIZER, useFactory: ngswAppInitializer, - deps: [Injector, SCRIPT, OPTS], + deps: [Injector, SCRIPT, RegistrationOptions], multi: true, }, ], }; } -} \ No newline at end of file +} diff --git a/tools/public_api_guard/service-worker/service-worker.d.ts b/tools/public_api_guard/service-worker/service-worker.d.ts index d3efcb131b..c70daacd72 100644 --- a/tools/public_api_guard/service-worker/service-worker.d.ts +++ b/tools/public_api_guard/service-worker/service-worker.d.ts @@ -1,6 +1,9 @@ /** @experimental */ export declare class ServiceWorkerModule { - static register(script: string, opts?: RegistrationOptions): ModuleWithProviders; + static register(script: string, opts?: { + scope?: string; + enabled?: boolean; + }): ModuleWithProviders; } /** @experimental */