1
1
import { initSentry , logError } from '../errors' ;
2
2
initSentry ( process . env . SENTRY_DSN ) ;
3
3
4
+ // Used elsewhere in server API requests later to get the auth token:
4
5
import * as localForage from 'localforage' ;
6
+ localForage . config ( { name : "httptoolkit" , version : 1 } ) ;
5
7
6
8
import { registerRoute , NavigationRoute } from 'workbox-routing' ;
7
9
import { PrecacheController } from 'workbox-precaching'
@@ -11,38 +13,8 @@ import { StaleWhileRevalidate, NetworkOnly } from 'workbox-strategies';
11
13
import packageMetadata from '../../package.json' ;
12
14
import { getServerVersion } from './server-api' ;
13
15
import { lastServerVersion , versionSatisfies } from './service-versions' ;
14
- import { delay } from '../util/promise' ;
15
16
16
17
const appVersion = process . env . UI_VERSION || "Unknown" ;
17
- localForage . config ( { name : "httptoolkit" , version : 1 } ) ;
18
-
19
- // Check if the server is accessible, and that we've been given the relevant auth
20
- // details. If we haven't, that means the desktop has started the server with an
21
- // auth token, but we haven't received it. In general, that means the UI hasn't
22
- // passed it on, because it's outdated and doesn't understand it. To fix this,
23
- // we forcibly update the UI immediately. Should only ever happen once.
24
-
25
- type ServerStatus = 'accessible' | 'auth-required' | 'inaccessible'
26
- const serverStatus : Promise < ServerStatus > =
27
- fetch ( "http://127.0.0.1:45457/" , { method : 'POST' } )
28
- . then ( ( response ) => {
29
- if ( response . status === 403 ) return 'auth-required' ;
30
- else return 'accessible' ;
31
- } )
32
- . catch ( ( ) => 'inaccessible' as ServerStatus )
33
- . then ( ( status ) => {
34
- console . log ( 'Service worker server status:' , status ) ;
35
- return status ;
36
- } ) ;
37
-
38
- const forceUpdateRequired = serverStatus . then ( async ( status ) => {
39
- // Update should be forced if we're using a server that requires auth,
40
- // the UI hasn't properly provided an auth token, and there is
41
- // currently an active service worker (i.e. a cached UI)
42
- return status === 'auth-required' &&
43
- ! ( await localForage . getItem < string > ( 'latest-auth-token' ) ) &&
44
- ! ! self . registration . active ;
45
- } ) ;
46
18
47
19
type PrecacheEntry = {
48
20
url : string ;
@@ -67,13 +39,6 @@ const precacheController = getPrecacheController();
67
39
const precacheName = precacheController . strategy . cacheName ;
68
40
69
41
async function precacheNewVersionIfSupported ( event : ExtendableEvent ) {
70
- if ( await forceUpdateRequired ) {
71
- // Don't bother precaching: we want to take over & then force kill/refresh everything ASAP
72
- self . skipWaiting ( ) ;
73
- logError ( "Force update required on newly installed SW" ) ;
74
- return ;
75
- }
76
-
77
42
await checkServerVersion ( ) ;
78
43
79
44
// Any required as the install return types haven't been updated for v4, so still use 'updatedEntries'
@@ -84,15 +49,6 @@ async function precacheNewVersionIfSupported(event: ExtendableEvent) {
84
49
async function checkServerVersion ( ) {
85
50
const serverVersion = await getServerVersion ( ) . catch ( async ( e ) => {
86
51
console . log ( "Failed to get server version. Fallback back to last version anyway..." ) ;
87
-
88
- console . log (
89
- "Version unavailable but not forcing update, why?" ,
90
- "status" , await serverStatus ,
91
- "got token" , ! ! ( await localForage . getItem < string > ( 'latest-auth-token' ) ) ,
92
- "SW registrations" , self . registration ,
93
- "active SW" , ! ! self . registration ?. active
94
- ) ;
95
-
96
52
logError ( e ) ;
97
53
98
54
// This isn't perfect, but it's a pretty good approximation of when it's safe to update
@@ -102,7 +58,7 @@ async function checkServerVersion() {
102
58
103
59
// This should never happen: the serverStatus checks should guarantee that we can
104
60
// talk to the server in almost all cases, or that we have cached data. Fail & report it.
105
- throw new Error ( "No server version available, even though server check passed " ) ;
61
+ throw new Error ( "No server version available in UI update worker " ) ;
106
62
} ) ;
107
63
108
64
console . log ( `Connected httptoolkit-server version is ${ serverVersion } .` ) ;
@@ -150,40 +106,17 @@ self.addEventListener('install', (event: ExtendableEvent) => {
150
106
} ) ;
151
107
152
108
self . addEventListener ( 'activate' , async ( event ) => {
109
+ console . log ( 'Update worker activating...' ) ;
153
110
event . waitUntil ( ( async ( ) => {
154
111
console . log ( `SW activating for version ${ appVersion } ` ) ;
155
112
156
- if ( await forceUpdateRequired ) {
157
- logError ( "Force update required on newly activated SW" ) ;
158
-
159
- resettingSw = true ; // Pass through all requests
160
-
161
- // Take over and refresh all client pages:
162
- await self . clients . claim ( ) ;
163
- const clients = await self . clients . matchAll ( { type : 'window' } ) ;
164
- clients . map ( ( client ) => {
165
- console . log ( `Refreshing ${ client . url } ` ) ;
166
- return ( client as WindowClient ) . navigate ( client . url )
167
- . catch ( async ( e ) => {
168
- // On the first error, try once more, after a brief delay. Sometimes
169
- // claim() might not process fast enough, and this is necessary.
170
- await delay ( 100 ) ;
171
- return ( client as WindowClient ) . navigate ( client . url ) ;
172
- } ) ;
173
- } ) ;
174
-
175
- // Unregister, so that future registration loads the SW & caches from scratch
176
- self . registration . unregister ( ) ;
177
- console . log ( "SW forcibly refreshed" ) ;
178
- } else {
179
- // This can be removed only once we know that _nobody_ is using SWs from before 2019-05-09
180
- deleteOldWorkboxCaches ( ) ;
181
-
182
- await precacheController . activate ( event ) ;
183
-
184
- // Delete the old (now unused) SW event logs
185
- indexedDB . deleteDatabase ( 'keyval-store' ) ;
186
- }
113
+ // This can be removed only once we know that _nobody_ is using SWs from before 2019-05-09
114
+ deleteOldWorkboxCaches ( ) ;
115
+
116
+ await precacheController . activate ( event ) ;
117
+
118
+ // Delete the old (now unused) SW event logs
119
+ indexedDB . deleteDatabase ( 'keyval-store' ) ;
187
120
} ) ( ) ) ;
188
121
} ) ;
189
122
0 commit comments