Skip to content

Commit e7e543e

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 (cherry picked from commit 6fa8856)
1 parent d3f8df1 commit e7e543e

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
@@ -93,8 +93,13 @@ describe('Service Worker Schematic', () => {
9393
const tree = await schematicRunner.runSchematicAsync('service-worker', defaultOptions, appTree)
9494
.toPromise();
9595
const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts');
96-
const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: environment.production })';
97-
expect(pkgText).toContain(expectedText);
96+
expect(pkgText).toMatch(new RegExp(
97+
'(\\s+)ServiceWorkerModule\\.register\\(\'ngsw-worker\\.js\', \\{\\n' +
98+
'\\1 enabled: environment\\.production,\\n' +
99+
'\\1 // Register the ServiceWorker as soon as the app is stable\\n' +
100+
'\\1 // or after 30 seconds \\(whichever comes first\\)\\.\\n' +
101+
'\\1 registrationStrategy: \'registerWhenStable:30000\'\\n' +
102+
'\\1}\\)'));
98103
});
99104

100105
it('should add the SW import to the NgModule imports with aliased environment', async () => {
@@ -122,8 +127,13 @@ describe('Service Worker Schematic', () => {
122127
const tree = await schematicRunner.runSchematicAsync('service-worker', defaultOptions, appTree)
123128
.toPromise();
124129
const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts');
125-
const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: env.production })';
126-
expect(pkgText).toContain(expectedText);
130+
expect(pkgText).toMatch(new RegExp(
131+
'(\\s+)ServiceWorkerModule\\.register\\(\'ngsw-worker\\.js\', \\{\\n' +
132+
'\\1 enabled: env\\.production,\\n' +
133+
'\\1 // Register the ServiceWorker as soon as the app is stable\\n' +
134+
'\\1 // or after 30 seconds \\(whichever comes first\\)\\.\\n' +
135+
'\\1 registrationStrategy: \'registerWhenStable:30000\'\\n' +
136+
'\\1}\\)'));
127137
});
128138

129139
it('should add the SW import to the NgModule imports with existing environment', async () => {
@@ -151,8 +161,13 @@ describe('Service Worker Schematic', () => {
151161
const tree = await schematicRunner.runSchematicAsync('service-worker', defaultOptions, appTree)
152162
.toPromise();
153163
const pkgText = tree.readContent('/projects/bar/src/app/app.module.ts');
154-
const expectedText = 'ServiceWorkerModule.register(\'ngsw-worker.js\', { enabled: environment.production })';
155-
expect(pkgText).toContain(expectedText);
164+
expect(pkgText).toMatch(new RegExp(
165+
'(\\s+)ServiceWorkerModule\\.register\\(\'ngsw-worker\\.js\', \\{\\n' +
166+
'\\1 enabled: environment\\.production,\\n' +
167+
'\\1 // Register the ServiceWorker as soon as the app is stable\\n' +
168+
'\\1 // or after 30 seconds \\(whichever comes first\\)\\.\\n' +
169+
'\\1 registrationStrategy: \'registerWhenStable:30000\'\\n' +
170+
'\\1}\\)'));
156171
});
157172

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

0 commit comments

Comments
 (0)