Skip to content

Commit 39c0152

Browse files
deeblooAndrewKushnir
deebloo
authored andcommittedApr 25, 2019
feat(service-worker): expose SwRegistrationOptions token to allow runtime config (#21842)
Previously, the ServiceWorker registration options should be defined as an object literal (in order for them to be compatible with Ahead-of-Time compilation), thus making it impossible to base the ServiceWorker behavior on runtime conditions. This commit allows specifying the registration options using a regular provider, which means that it can take advantage of the `useFactory` option to determine the config at runtime, while still remaining compatible with AoT compilation. PR Close #21842
1 parent d7887ab commit 39c0152

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed
 

‎packages/service-worker/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
export {UpdateActivatedEvent, UpdateAvailableEvent} from './low_level';
18-
export {ServiceWorkerModule} from './module';
18+
export {ServiceWorkerModule, SwRegistrationOptions} from './module';
1919
export {SwPush} from './push';
2020
export {SwUpdate} from './update';

‎packages/service-worker/src/module.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import {NgswCommChannel} from './low_level';
1414
import {SwPush} from './push';
1515
import {SwUpdate} from './update';
1616

17+
/**
18+
* Token that can be used to provide options for `ServiceWorkerModule` outside of
19+
* `ServiceWorkerModule.register()`.
20+
*
21+
* @publicApi
22+
*/
1723
export abstract class SwRegistrationOptions {
1824
scope?: string;
1925
enabled?: boolean;
@@ -69,7 +75,7 @@ export class ServiceWorkerModule {
6975
* If `enabled` is set to `false` in the given options, the module will behave as if service
7076
* workers are not supported by the browser, and the service worker will not be registered.
7177
*/
72-
static register(script: string, opts: {scope?: string; enabled?: boolean;} = {}):
78+
static register(script: string, opts: SwRegistrationOptions = {}):
7379
ModuleWithProviders<ServiceWorkerModule> {
7480
return {
7581
ngModule: ServiceWorkerModule,

‎packages/service-worker/test/comm_spec.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {PLATFORM_ID} from '@angular/core';
1010
import {TestBed} from '@angular/core/testing';
1111
import {NgswCommChannel} from '@angular/service-worker/src/low_level';
12-
import {SwRegistrationOptions, ngswCommChannelFactory} from '@angular/service-worker/src/module';
12+
import {ServiceWorkerModule, SwRegistrationOptions, ngswCommChannelFactory} from '@angular/service-worker/src/module';
1313
import {SwPush} from '@angular/service-worker/src/push';
1414
import {SwUpdate} from '@angular/service-worker/src/update';
1515
import {MockPushManager, MockPushSubscription, MockServiceWorkerContainer, MockServiceWorkerRegistration, patchDecodeBase64} from '@angular/service-worker/testing/mock';
@@ -47,6 +47,45 @@ import {async_fit, async_it} from './async';
4747
});
4848
});
4949

50+
describe('ServiceWorkerModule config', () => {
51+
it('SwUpdate isEnabled is false when configuring via static method', () => {
52+
TestBed.configureTestingModule(
53+
{imports: [ServiceWorkerModule.register('', {enabled: false})]});
54+
55+
expect(TestBed.get(SwUpdate).isEnabled).toEqual(false);
56+
});
57+
58+
it('SwUpdate isEnabled is true when configuring via static method', () => {
59+
TestBed.configureTestingModule({
60+
imports: [ServiceWorkerModule.register('', {enabled: true})],
61+
providers: [{provide: NgswCommChannel, useValue: comm}]
62+
});
63+
64+
expect(TestBed.get(SwUpdate).isEnabled).toEqual(true);
65+
});
66+
67+
it('SwUpdate isEnabled is false when configuring directly via token', () => {
68+
TestBed.configureTestingModule({
69+
imports: [ServiceWorkerModule.register('')],
70+
providers: [{provide: SwRegistrationOptions, useFactory: () => ({enabled: false})}]
71+
});
72+
73+
expect(TestBed.get(SwUpdate).isEnabled).toEqual(false);
74+
});
75+
76+
it('SwUpdate isEnabled is true when configuring directly via token', () => {
77+
TestBed.configureTestingModule({
78+
imports: [ServiceWorkerModule.register('')],
79+
providers: [
80+
{provide: NgswCommChannel, useValue: comm},
81+
{provide: SwRegistrationOptions, useFactory: () => ({enabled: true})}
82+
]
83+
});
84+
85+
expect(TestBed.get(SwUpdate).isEnabled).toEqual(true);
86+
});
87+
});
88+
5089
describe('ngswCommChannelFactory', () => {
5190
it('gives disabled NgswCommChannel for platform-server', () => {
5291
TestBed.configureTestingModule({

‎tools/public_api_guard/service-worker/service-worker.d.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
export declare class ServiceWorkerModule {
2-
static register(script: string, opts?: {
3-
scope?: string;
4-
enabled?: boolean;
5-
}): ModuleWithProviders<ServiceWorkerModule>;
2+
static register(script: string, opts?: SwRegistrationOptions): ModuleWithProviders<ServiceWorkerModule>;
63
}
74

85
export declare class SwPush {
@@ -22,6 +19,11 @@ export declare class SwPush {
2219
unsubscribe(): Promise<void>;
2320
}
2421

22+
export declare abstract class SwRegistrationOptions {
23+
enabled?: boolean;
24+
scope?: string;
25+
}
26+
2527
export declare class SwUpdate {
2628
readonly activated: Observable<UpdateActivatedEvent>;
2729
readonly available: Observable<UpdateAvailableEvent>;

0 commit comments

Comments
 (0)
Please sign in to comment.