Skip to content

Commit 29e8a64

Browse files
sonukapooralxhub
authored andcommitted
fix(service-worker): by default register the SW after 30s even the app never stabilizes (#35870)
Previously, when using the default ServiceWorker registration strategy Angular would wait indefinitely for the [app to stabilize][1], before registering the ServiceWorker script. This could lead to a situation where the ServiceWorker would never be registered when there was a long-running task (such as an interval or recurring timeout). Such tasks can often be started by a 3rd-party dependency (beyond the developer's control or even without them realizing). In addition, this situation is particularly hard to detect, because the ServiceWorker is typically not used during development and on production builds a previous ServiceWorker instance might be already active. This commit fixes this by changing the default registration strategy from `registerWhenStable` to `registerWhenStable:30000`, which will ensure that the ServiceWorker will be registered after 30s at the latest, even if the app has not stabilized by then. Fixes #34464 PR Close #35870
1 parent 00efacf commit 29e8a64

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/service-worker/src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ export function ngswAppInitializer(
100100
if (typeof options.registrationStrategy === 'function') {
101101
readyToRegister$ = options.registrationStrategy();
102102
} else {
103-
const [strategy, ...args] = (options.registrationStrategy || 'registerWhenStable').split(':');
103+
const [strategy, ...args] =
104+
(options.registrationStrategy || 'registerWhenStable:30000').split(':');
105+
104106
switch (strategy) {
105107
case 'registerImmediately':
106108
readyToRegister$ = of (null);

packages/service-worker/test/module_spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('ServiceWorkerModule', () => {
147147
return isStableSub;
148148
};
149149

150-
it('defaults to registering the SW when the app stabilizes', fakeAsync(() => {
150+
it('defaults to registering the SW when the app stabilizes (under 30s)', fakeAsync(() => {
151151
const isStableSub = configTestBedWithMockedStability();
152152

153153
isStableSub.next(false);
@@ -156,7 +156,7 @@ describe('ServiceWorkerModule', () => {
156156
tick();
157157
expect(swRegisterSpy).not.toHaveBeenCalled();
158158

159-
tick(60000);
159+
tick(20000);
160160
expect(swRegisterSpy).not.toHaveBeenCalled();
161161

162162
isStableSub.next(true);
@@ -165,6 +165,17 @@ describe('ServiceWorkerModule', () => {
165165
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: undefined});
166166
}));
167167

168+
it('defaults to registering the SW after 30s if the app does not stabilize sooner',
169+
fakeAsync(() => {
170+
const isStableSub = configTestBedWithMockedStability();
171+
172+
tick(29999);
173+
expect(swRegisterSpy).not.toHaveBeenCalled();
174+
175+
tick(1);
176+
expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: undefined});
177+
}));
178+
168179
it('registers the SW when the app stabilizes with `registerWhenStable:<timeout>`',
169180
fakeAsync(() => {
170181
const isStableSub = configTestBedWithMockedStability('registerWhenStable:1000');

0 commit comments

Comments
 (0)