fix(service-worker): ensure initialised in browser only (#20782)

closes #20360

PR Close #20782
This commit is contained in:
Fabian Wiles
2017-11-12 12:39:27 +13:00
committed by Igor Minar
parent da3563ce19
commit 7cabaa0ae7
7 changed files with 39 additions and 18 deletions

View File

@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Injectable} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Observable} from 'rxjs/Observable';
import {ConnectableObservable} from 'rxjs/observable/ConnectableObservable';
@ -86,8 +87,11 @@ export class NgswCommChannel {
*/
readonly events: Observable<IncomingEvent>;
constructor(private serviceWorker: ServiceWorkerContainer|undefined) {
if (!serviceWorker) {
constructor(
private serviceWorker: ServiceWorkerContainer|undefined,
@Inject(PLATFORM_ID) platformId: string) {
if (!serviceWorker || !isPlatformBrowser(platformId)) {
this.serviceWorker = undefined;
this.worker = this.events = this.registration = errorObservable(ERR_SW_NOT_SUPPORTED);
} else {
const controllerChangeEvents =

View File

@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {APP_INITIALIZER, ApplicationRef, Inject, InjectionToken, Injector, ModuleWithProviders, NgModule} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
import {APP_INITIALIZER, ApplicationRef, Inject, InjectionToken, Injector, ModuleWithProviders, NgModule, PLATFORM_ID} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {filter as op_filter} from 'rxjs/operator/filter';
import {take as op_take} from 'rxjs/operator/take';
@ -24,10 +25,12 @@ export abstract class RegistrationOptions {
export const SCRIPT = new InjectionToken<string>('NGSW_REGISTER_SCRIPT');
export function ngswAppInitializer(
injector: Injector, script: string, options: RegistrationOptions): Function {
injector: Injector, script: string, options: RegistrationOptions,
platformId: string): Function {
const initializer = () => {
const app = injector.get<ApplicationRef>(ApplicationRef);
if (!('serviceWorker' in navigator) || options.enabled === false) {
if (!(isPlatformBrowser(platformId) && ('serviceWorker' in navigator) &&
options.enabled !== false)) {
return;
}
const onStable =
@ -51,8 +54,10 @@ export function ngswAppInitializer(
return initializer;
}
export function ngswCommChannelFactory(opts: RegistrationOptions): NgswCommChannel {
return new NgswCommChannel(opts.enabled !== false ? navigator.serviceWorker : undefined);
export function ngswCommChannelFactory(
opts: RegistrationOptions, platformId: string): NgswCommChannel {
return new NgswCommChannel(
opts.enabled !== false ? navigator.serviceWorker : undefined, platformId);
}
/**
@ -75,11 +80,15 @@ export class ServiceWorkerModule {
providers: [
{provide: SCRIPT, useValue: script},
{provide: RegistrationOptions, useValue: opts},
{provide: NgswCommChannel, useFactory: ngswCommChannelFactory, deps: [RegistrationOptions]},
{
provide: NgswCommChannel,
useFactory: ngswCommChannelFactory,
deps: [RegistrationOptions, PLATFORM_ID]
},
{
provide: APP_INITIALIZER,
useFactory: ngswAppInitializer,
deps: [Injector, SCRIPT, RegistrationOptions],
deps: [Injector, SCRIPT, RegistrationOptions, PLATFORM_ID],
multi: true,
},
],