Skip to content

Commit b93fa22

Browse files
arturovtkirjs
authored andcommitted
fix(service-worker): prevent duplicate fetches during concurrent update checks (#61443)
Previously, multiple simultaneous calls to `checkForUpdate()` could result in redundant fetches and hashing of the update manifest, leading to unnecessary network and CPU usage. This change introduces a mechanism to track an in-progress update check using a cached promise (`ongoingCheckForUpdate`). Subsequent calls to `checkForUpdate()` while a check is in progress will return the same promise instead of triggering a new request. Once the check completes (successfully or not), the cached promise is cleared, allowing future update checks to proceed normally. This improves efficiency and prevents overlapping update logic in applications that may invoke `checkForUpdate()` from multiple sources (e.g. polling, manual triggers). PR Close #61443
1 parent 939ca07 commit b93fa22

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/service-worker/src/update.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export class SwUpdate {
5353
return this.sw.isEnabled;
5454
}
5555

56+
private ongoingCheckForUpdate: Promise<boolean> | null = null;
57+
5658
constructor(private sw: NgswCommChannel) {
5759
if (!sw.isEnabled) {
5860
this.versionUpdates = NEVER;
@@ -81,8 +83,16 @@ export class SwUpdate {
8183
if (!this.sw.isEnabled) {
8284
return Promise.reject(new Error(ERR_SW_NOT_SUPPORTED));
8385
}
86+
if (this.ongoingCheckForUpdate) {
87+
return this.ongoingCheckForUpdate;
88+
}
8489
const nonce = this.sw.generateNonce();
85-
return this.sw.postMessageWithOperation('CHECK_FOR_UPDATES', {nonce}, nonce);
90+
this.ongoingCheckForUpdate = this.sw
91+
.postMessageWithOperation('CHECK_FOR_UPDATES', {nonce}, nonce)
92+
.finally(() => {
93+
this.ongoingCheckForUpdate = null;
94+
});
95+
return this.ongoingCheckForUpdate;
8696
}
8797

8898
/**

0 commit comments

Comments
 (0)