Skip to content

Commit 65c434d

Browse files
committed
Silently apply updates instead of asking user to confirm
1 parent 8aaee52 commit 65c434d

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
import { ServiceWorkerHandler as BaseHandler } from "hydrogen-web/src/platform/web/dom/ServiceWorkerHandler.js";
22

33
export class ServiceWorkerHandler extends BaseHandler {
4+
private get registration() {
5+
return super.getRegistration();
6+
}
7+
48
public registerAndStart(path: string) {
59
super.registerAndStart(path);
610
}
711

812
public async preventConcurrentSessionAccess(sessionId: string) {
9-
return super._sendAndWaitForReply("closeSession", { sessionId });
13+
return this._sendAndWaitForReply("closeSession", { sessionId });
14+
}
15+
16+
async _proposeUpdate() {
17+
if (document.hidden) {
18+
return;
19+
}
20+
21+
const version = await this._sendAndWaitForReply("version", null, this.registration.waiting);
22+
23+
// Prevent any fetch requests from going to the service worker from any client,
24+
// so that it's not kept active when calling skipWaiting on the new one.
25+
await this._sendAndWaitForReply("haltRequests", null);
26+
27+
console.log(`Updating to version ${version.version} (${version.buildHash})`);
28+
29+
// Once all requests are blocked, ask the new service worker to skipWaiting.
30+
return this._send("skipWaiting", null, this.registration.waiting);
31+
}
32+
33+
async _sendAndWaitForReply(type, payload: {}|null, worker = undefined) {
34+
return super._sendAndWaitForReply(type, payload, worker);
35+
}
36+
37+
async _send(type, payload, worker = undefined) {
38+
return super._send(type, payload, worker);
1039
}
1140
}

0 commit comments

Comments
 (0)