Skip to content

Commit 6fa8856

Browse files
gkalpakfilipesilva
authored andcommitted
fix(@schematics/angular): explicitly specify ServiceWorker registration strategy
This commit updates the `service-worker` schematics to explicitly specify the ServiceWorker registration strategy in the [ServiceWorkerModule.register()] call. We still use the default strategy, so there should be no change in the behavior of the generated apps. However, it will help people find out what the default behavior is when debugging potential issues with delayed ServiceWorker registration. (See the discussion in angular/angular#41223 for more details.) [1]: https://angular.io/api/service-worker/ServiceWorkerModule#register
1 parent fb14945 commit 6fa8856

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

packages/schematics/angular/service-worker/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { join, normalize } from '@angular-devkit/core';
8+
import { join, normalize, tags } from '@angular-devkit/core';
99
import {
1010
Rule,
1111
SchematicContext,
@@ -90,8 +90,14 @@ function updateAppModule(mainPath: string): Rule {
9090
}
9191

9292
// register SW in app module
93-
const importText =
94-
`ServiceWorkerModule.register('ngsw-worker.js', { enabled: ${importModule}.production })`;
93+
const importText = tags.stripIndent`
94+
ServiceWorkerModule.register('ngsw-worker.js', {
95+
enabled: ${importModule}.production,
96+
// Register the ServiceWorker as soon as the app is stable
97+
// or after 30 seconds (whichever comes first).
98+
registrationStrategy: 'registerWhenStable:30000'
99+
})
100+
`;
95101
moduleSource = getTsSourceFile(host, modulePath);
96102
const metadataChanges = addSymbolToNgModuleMetadata(
97103
moduleSource, modulePath, 'imports', importText);

packages/schematics/angular/service-worker/index_spec.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ describe('Service Worker Schematic', () => {
8181
const tree = await schematicRunner.runSchematicAsync('service-worker', defaultOptions, appTree)
8282
.toPromise();
8383
const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts');
84-
const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: environment.production })';
85-
expect(pkgText).toContain(expectedText);
84+
expect(pkgText).toMatch(new RegExp(
85+
'(\\s+)ServiceWorkerModule\\.register\\(\'ngsw-worker\\.js\', \\{\\n' +
86+
'\\1 enabled: environment\\.production,\\n' +
87+
'\\1 // Register the ServiceWorker as soon as the app is stable\\n' +
88+
'\\1 // or after 30 seconds \\(whichever comes first\\)\\.\\n' +
89+
'\\1 registrationStrategy: \'registerWhenStable:30000\'\\n' +
90+
'\\1}\\)'));
8691
});
8792

8893
it('should add the SW import to the NgModule imports with aliased environment', async () => {
@@ -110,8 +115,13 @@ describe('Service Worker Schematic', () => {
110115
const tree = await schematicRunner.runSchematicAsync('service-worker', defaultOptions, appTree)
111116
.toPromise();
112117
const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts');
113-
const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: env.production })';
114-
expect(pkgText).toContain(expectedText);
118+
expect(pkgText).toMatch(new RegExp(
119+
'(\\s+)ServiceWorkerModule\\.register\\(\'ngsw-worker\\.js\', \\{\\n' +
120+
'\\1 enabled: env\\.production,\\n' +
121+
'\\1 // Register the ServiceWorker as soon as the app is stable\\n' +
122+
'\\1 // or after 30 seconds \\(whichever comes first\\)\\.\\n' +
123+
'\\1 registrationStrategy: \'registerWhenStable:30000\'\\n' +
124+
'\\1}\\)'));
115125
});
116126

117127
it('should add the SW import to the NgModule imports with existing environment', async () => {
@@ -139,8 +149,13 @@ describe('Service Worker Schematic', () => {
139149
const tree = await schematicRunner.runSchematicAsync('service-worker', defaultOptions, appTree)
140150
.toPromise();
141151
const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts');
142-
const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: environment.production })';
143-
expect(pkgText).toContain(expectedText);
152+
expect(pkgText).toMatch(new RegExp(
153+
'(\\s+)ServiceWorkerModule\\.register\\(\'ngsw-worker\\.js\', \\{\\n' +
154+
'\\1 enabled: environment\\.production,\\n' +
155+
'\\1 // Register the ServiceWorker as soon as the app is stable\\n' +
156+
'\\1 // or after 30 seconds \\(whichever comes first\\)\\.\\n' +
157+
'\\1 registrationStrategy: \'registerWhenStable:30000\'\\n' +
158+
'\\1}\\)'));
144159
});
145160

146161
it('should put the ngsw-config.json file in the project root', async () => {

0 commit comments

Comments
 (0)