Skip to content

Commit 624b5fb

Browse files
committed
Reorganize app update logic slightly
This also more quietly handles failed server app update requests, and gives us a nice space to do more advanced update logic (coming soon).
1 parent e7938f0 commit 624b5fb

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

automation/webpack.prod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export default merge(common, {
4242
swSrc: path.join(
4343
path.dirname(common.entry as string),
4444
'services',
45-
'update-worker.ts'
45+
'ui-update-worker.ts'
4646
),
47-
exclude: ['google-fonts', /^api\//, 'update-worker.js', /.map$/],
47+
exclude: ['google-fonts', /^api\//, 'ui-update-worker.js', /.map$/],
4848
maximumFileSizeToCacheInBytes: 100 * 1024 * 1024,
4949
manifestTransforms: [
5050
(originalManifest: any, compilation: any) => {

src/index.tsx

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import { delay } from './util/promise';
2121
import { initMetrics } from './metrics';
2222
import { appHistory } from './routing';
2323

24-
import registerUpdateWorker, { ServiceWorkerNoSupportError } from 'service-worker-loader!./services/update-worker';
25-
2624
import { HttpExchange } from './types';
2725

2826
import { UiStore } from './model/ui/ui-store';
@@ -34,8 +32,11 @@ import { InterceptorStore } from './model/interception/interceptor-store';
3432
import { ApiStore } from './model/api/api-store';
3533
import { SendStore } from './model/send/send-store';
3634

37-
import { triggerServerUpdate } from './services/server-api';
3835
import { serverVersion, lastServerVersion, UI_VERSION } from './services/service-versions';
36+
import {
37+
attemptServerUpdate,
38+
runBackgroundUpdates
39+
} from './services/update-management';
3940

4041
import { App } from './components/app';
4142
import { StorePoweredThemeProvider } from './components/store-powered-theme-provider';
@@ -47,31 +48,10 @@ const APP_ELEMENT_SELECTOR = '#app';
4748

4849
mobx.configure({ enforceActions: 'observed' });
4950

50-
// Set up a SW in the background to add offline support & instant startup.
51-
// This also checks for new versions after the first SW is already live.
52-
// Slightly delayed to avoid competing for bandwidth with startup on slow connections.
53-
delay(5000).then(() => {
54-
// Try to trigger a server update. Can't guarantee it'll work, and we also trigger
55-
// after successful startup, but this tries to ensure that even if startup is broken,
56-
// we still update the server (and hopefully thereby unbreak app startup).
57-
triggerServerUpdate();
58-
return registerUpdateWorker({ scope: '/' });
59-
}).then((registration) => {
60-
console.log('Service worker loaded');
61-
registration.update().catch(console.log);
62-
63-
// Check for SW updates every 5 minutes.
64-
setInterval(() => {
65-
triggerServerUpdate();
66-
registration.update().catch(console.log);
67-
}, 1000 * 60 * 5);
68-
})
69-
.catch((e) => {
70-
if (e instanceof ServiceWorkerNoSupportError) {
71-
console.log('Service worker not supported, oh well, no autoupdating for you.');
72-
}
73-
throw e;
74-
});
51+
// Begin checking for updates and pre-caching UI components we might need, to support
52+
// background updates & instant offline startup. We slightly delay this to avoid
53+
// competing for bandwidth/CPU/etc with startup on slow connections.
54+
delay(5000).then(runBackgroundUpdates);
7555

7656
const accountStore = new AccountStore(
7757
() => appHistory.navigate('/settings')
@@ -121,7 +101,7 @@ initMetrics();
121101
// Once the app is loaded, show the app
122102
appStartupPromise.then(() => {
123103
// We now know that the server is running - tell it to check for updates
124-
triggerServerUpdate();
104+
attemptServerUpdate();
125105

126106
console.log('App started, rendering');
127107

src/services/update-worker.ts renamed to src/services/ui-update-worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { PrecacheController } from 'workbox-precaching'
88
import { ExpirationPlugin } from 'workbox-expiration';
99
import { StaleWhileRevalidate, NetworkOnly } from 'workbox-strategies';
1010

11-
import * as appPackage from '../../package.json';
11+
import * as packageMetadata from '../../package.json';
1212
import { getServerVersion } from './server-api';
1313
import { lastServerVersion, versionSatisfies } from './service-versions';
1414
import { delay } from '../util/promise';
@@ -107,10 +107,10 @@ async function checkServerVersion() {
107107

108108
console.log(`Connected httptoolkit-server version is ${serverVersion}.`);
109109
console.log(`App requires server version satisfying ${
110-
appPackage.runtimeDependencies['httptoolkit-server']
110+
packageMetadata.runtimeDependencies['httptoolkit-server']
111111
}.`);
112112

113-
if (!versionSatisfies(serverVersion, appPackage.runtimeDependencies['httptoolkit-server'])) {
113+
if (!versionSatisfies(serverVersion, packageMetadata.runtimeDependencies['httptoolkit-server'])) {
114114
throw new Error(
115115
`New app version ${appVersion} available, but server ${
116116
await serverVersion

src/services/update-management.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import registerUpdateWorker, {
2+
ServiceWorkerNoSupportError
3+
} from 'service-worker-loader!./ui-update-worker';
4+
5+
import { triggerServerUpdate } from './server-api';
6+
7+
export const attemptServerUpdate = () =>
8+
triggerServerUpdate().catch(console.warn);
9+
10+
// Set up a SW in the background, to add offline support & instant startup.
11+
// This also checks for new UI & server versions at intervals.
12+
export async function runBackgroundUpdates() {
13+
// Try to trigger a server update. Can't guarantee it'll work, and we also trigger it
14+
// after successful startup, but this tries to ensure that even if startup is broken,
15+
// we still update the server (and hopefully thereby unbreak app startup).
16+
attemptServerUpdate();
17+
18+
try {
19+
const registration = await registerUpdateWorker({ scope: '/' });
20+
console.log('Service worker loaded');
21+
registration.update().catch(console.log);
22+
23+
// Check for server & UI updates every 5 minutes:
24+
setInterval(() => {
25+
attemptServerUpdate();
26+
registration.update().catch(console.log);
27+
}, 1000 * 60 * 5);
28+
} catch (e) {
29+
if (e instanceof ServiceWorkerNoSupportError) {
30+
console.log('Service worker not supported, oh well, no autoupdating for you.');
31+
}
32+
throw e;
33+
}
34+
}

0 commit comments

Comments
 (0)