Skip to content

Commit 3a2d181

Browse files
committed
fix
1 parent b8efe19 commit 3a2d181

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Scripts/app.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ class App {
7474
};
7575
};
7676
//#endif
77-
78-
public static async forceUpdate() {
79-
const bswup = (window as any).BitBswup;
80-
const bswupProgress = (window as any).BitBswupProgress;
81-
82-
if (await bswup.skipWaiting()) return;
83-
84-
bswupProgress.config({ autoReload: true });
85-
bswup.checkForUpdate();
86-
}
8777
}
8878

8979
(window as any).App = App;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Checks for and applies updates if available.
3+
* Called by `WebAppUpdateService.cs` when the user clicks the app version in `AppShell.razor`
4+
* or when `ForceUpdateSnackbar.razor` appears after a forced update.
5+
*/
6+
async function tryUpdatingPwa(autoReload?: boolean) {
7+
const bswup = (window as any).BitBswup; // https://bitplatform.dev/bswup
8+
const bswupProgress = (window as any).BitBswupProgress;
9+
10+
if (await bswup.skipWaiting()) return;
11+
12+
bswupProgress.config({ autoReload: autoReload ?? true });
13+
bswup.checkForUpdate();
14+
}
15+
16+
// To minimize user-facing force updates, the following code attempts to update the PWA ASAP.
17+
window.addEventListener('beforeunload', () => tryUpdatingPwa(true));
18+
19+
let appIsInBackground = false; // App is in the background if visibilityState is 'hidden' for 20 seconds.
20+
let visibilityChangeTimeout: number;
21+
window.addEventListener('visibilitychange', () => {
22+
if (document.visibilityState === 'hidden') {
23+
visibilityChangeTimeout = window.setTimeout(() => {
24+
appIsInBackground = true;
25+
}, 20 * 1000 /* 20 seconds */);
26+
} else {
27+
clearTimeout(visibilityChangeTimeout);
28+
appIsInBackground = false;
29+
}
30+
});
31+
32+
// This would download the updates, but reloads the app only if it's in the background.
33+
setInterval(() => tryUpdatingPwa(appIsInBackground), 60 * 1000 /* 1 minute */);

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Services/WebAppUpdateService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public partial class WebAppUpdateService : IAppUpdateService
77

88
public async Task ForceUpdate()
99
{
10-
await jsRuntime.InvokeVoidAsync("tryUpdatingPWA");
10+
await jsRuntime.InvokeVoidAsync("tryUpdatingPwa");
1111
}
1212
}

0 commit comments

Comments
 (0)