Skip to content

Commit 2897070

Browse files
authored
fix(EAR-3809): Start service worker prior to page load. (#6369)
* fix(EAR-3809): service worker is running in prod, but not intercepting fetch requests. Per mdn service work lifecycle docs, attempting to load service worker prior to page load -- to ensure is properly set. * chore(ear-3809): jest unit tests needs to have module mapped import for service worker * chore: update unit test mocks
1 parent dcaf47d commit 2897070

File tree

8 files changed

+51
-9
lines changed

8 files changed

+51
-9
lines changed

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = {
1919
'react-dnd$': 'react-dnd/dist/cjs',
2020
'react-dnd-html5-backend$': 'react-dnd-html5-backend/dist/cjs',
2121
'dnd-core$': 'dnd-core/dist/cjs',
22+
'worker-plugin/loader!../workers/downloadHelper':
23+
'<rootDir>/src/shared/workers/downloadHelper',
2224
},
2325
globals: {
2426
'ts-jest': {

src/dashboards/selectors/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ describe('Dashboards.Selector', () => {
115115
versionInfo: {version: '', commit: ''},
116116
flowsCTA: {explorer: true, alerts: true, tasks: true},
117117
subscriptionsCertificateInterest: false,
118+
workerRegistration: null,
118119
},
119120
}
120121

@@ -140,6 +141,7 @@ describe('Dashboards.Selector', () => {
140141
versionInfo: {version: '', commit: ''},
141142
flowsCTA: {explorer: true, alerts: true, tasks: true},
142143
subscriptionsCertificateInterest: false,
144+
workerRegistration: null,
143145
},
144146
}
145147

@@ -173,6 +175,7 @@ describe('Dashboards.Selector', () => {
173175
versionInfo: {version: '', commit: ''},
174176
flowsCTA: {explorer: true, alerts: true, tasks: true},
175177
subscriptionsCertificateInterest: false,
178+
workerRegistration: null,
176179
},
177180
}
178181

@@ -206,6 +209,7 @@ describe('Dashboards.Selector', () => {
206209
versionInfo: {version: '', commit: ''},
207210
flowsCTA: {explorer: true, alerts: true, tasks: true},
208211
subscriptionsCertificateInterest: false,
212+
workerRegistration: null,
209213
},
210214
}
211215

@@ -235,6 +239,7 @@ describe('Dashboards.Selector', () => {
235239
versionInfo: {version: '', commit: ''},
236240
flowsCTA: {explorer: true, alerts: true, tasks: true},
237241
subscriptionsCertificateInterest: false,
242+
workerRegistration: null,
238243
},
239244
}
240245

src/mockState.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const localState: LocalStorage = {
3636
},
3737
flowsCTA: {alerts: true, explorer: true, tasks: true},
3838
subscriptionsCertificateInterest: false,
39+
workerRegistration: null,
3940
},
4041
},
4142
flags: {

src/shared/actions/app.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export enum ActionTypes {
1212
SetFlowsCTA = 'SET_FLOWS_CTA',
1313
Noop = 'NOOP',
1414
SetSubscriptionsCertificateInterest = 'SET_SUB_CERT_INTEREST',
15+
SetWorkerRegistration = 'SET_WORKER_REGISTRATION',
1516
}
1617

1718
export type Action =
@@ -25,6 +26,7 @@ export type Action =
2526
| ReturnType<typeof setVersionInfo>
2627
| ReturnType<typeof setFlowsCTA>
2728
| ReturnType<typeof setSubscriptionsCertificateInterest>
29+
| ReturnType<typeof setWorkerRegistration>
2830

2931
// ephemeral state action creators
3032

@@ -84,3 +86,11 @@ export const setSubscriptionsCertificateInterest = () =>
8486
({
8587
type: ActionTypes.SetSubscriptionsCertificateInterest,
8688
} as const)
89+
90+
export const setWorkerRegistration = (
91+
workerRegistration: Promise<ServiceWorkerRegistration>
92+
) =>
93+
({
94+
type: ActionTypes.SetWorkerRegistration,
95+
payload: {workerRegistration},
96+
} as const)

src/shared/components/CSVExportButton.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import {runDownloadQuery} from 'src/timeMachine/actions/queries'
1212
// Types
1313
import {AppState} from 'src/types'
1414

15-
// Worker
16-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
17-
// @ts-ignore
18-
import downloadWorker from 'worker-plugin/loader!../workers/downloadHelper'
19-
2015
type Props = ConnectedProps<typeof connector>
2116

2217
interface State {
@@ -28,8 +23,8 @@ class CSVExportButton extends PureComponent<Props, State> {
2823
super(props)
2924
this.state = {browserSupportsDownload: false}
3025

31-
if ('serviceWorker' in navigator) {
32-
navigator.serviceWorker.register(downloadWorker).then(
26+
if (props.workerRegistration) {
27+
;(props.workerRegistration as Promise<ServiceWorkerRegistration>).then(
3328
() => this.setState({browserSupportsDownload: true}),
3429
function (err) {
3530
console.error(
@@ -81,7 +76,7 @@ const mstp = (state: AppState) => {
8176
const activeQueryText = getActiveQuery(state).text
8277
const disabled = activeQueryText === ''
8378

84-
return {disabled}
79+
return {disabled, workerRegistration: state.app.persisted.workerRegistration}
8580
}
8681

8782
const mdtp = {

src/shared/contexts/app.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {FC, useCallback} from 'react'
1+
import React, {FC, useCallback, useEffect} from 'react'
22
import {useSelector, useDispatch} from 'react-redux'
33

44
import {
@@ -10,6 +10,7 @@ import {
1010
disablePresentationMode,
1111
setFlowsCTA as setFlowsCTAAction,
1212
setSubscriptionsCertificateInterest as setSubscriptionsCertificateInterestAction,
13+
setWorkerRegistration,
1314
} from 'src/shared/actions/app'
1415
import {
1516
timeZone as timeZoneFromState,
@@ -27,6 +28,17 @@ import {presentationMode as presentationModeCopy} from 'src/shared/copy/notifica
2728
import {AppState, TimeZone, Theme, NavBarState, FlowsCTA} from 'src/types'
2829
import {event} from 'src/cloud/utils/reporting'
2930

31+
// Worker -- load prior to page load event, in order to intercept fetch in http/2.
32+
// see service worker life cycle. https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
33+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
34+
// @ts-ignore
35+
import downloadWorker from 'worker-plugin/loader!../workers/downloadHelper'
36+
37+
let workerRegistration = null
38+
if ('serviceWorker' in navigator) {
39+
workerRegistration = navigator.serviceWorker.register(downloadWorker)
40+
}
41+
3042
interface AppSettingContextType {
3143
timeZone: TimeZone
3244
theme: Theme
@@ -35,6 +47,7 @@ interface AppSettingContextType {
3547
navbarMode: NavBarState
3648
flowsCTA: FlowsCTA
3749
subscriptionsCertificateInterest: boolean
50+
workerRegistration: Promise<ServiceWorkerRegistration>
3851

3952
setTimeZone: (zone: TimeZone) => void
4053
setTheme: (theme: Theme) => void
@@ -53,6 +66,7 @@ const DEFAULT_CONTEXT: AppSettingContextType = {
5366
navbarMode: 'collapsed' as NavBarState,
5467
flowsCTA: {alerts: true, explorer: true, tasks: true} as FlowsCTA,
5568
subscriptionsCertificateInterest: false,
69+
workerRegistration,
5670

5771
setTimeZone: (_zone: TimeZone) => {},
5872
setTheme: (_theme: Theme) => {},
@@ -135,6 +149,10 @@ export const AppSettingProvider: FC = ({children}) => {
135149
dispatch(setSubscriptionsCertificateInterestAction())
136150
}, [dispatch])
137151

152+
useEffect(() => {
153+
dispatch(setWorkerRegistration(workerRegistration))
154+
}, [workerRegistration])
155+
138156
return (
139157
<AppSettingContext.Provider
140158
value={{
@@ -145,6 +163,7 @@ export const AppSettingProvider: FC = ({children}) => {
145163
navbarMode,
146164
flowsCTA,
147165
subscriptionsCertificateInterest,
166+
workerRegistration,
148167

149168
setTimeZone,
150169
setTheme,

src/shared/reducers/app.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('Shared.Reducers.appReducer', () => {
2525
versionInfo: {version: '', commit: ''},
2626
flowsCTA: {explorer: true, tasks: true, alerts: true},
2727
subscriptionsCertificateInterest: false,
28+
workerRegistration: null,
2829
},
2930
}
3031

src/shared/reducers/app.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface AppState {
1919
versionInfo: VersionInfo
2020
flowsCTA: FlowsCTA
2121
subscriptionsCertificateInterest: boolean
22+
workerRegistration: Promise<ServiceWorkerRegistration>
2223
}
2324
}
2425

@@ -36,6 +37,7 @@ const initialState: AppState = {
3637
versionInfo: {version: '', commit: ''},
3738
flowsCTA: {explorer: true, tasks: true, alerts: true},
3839
subscriptionsCertificateInterest: false,
40+
workerRegistration: null,
3941
},
4042
}
4143

@@ -126,6 +128,13 @@ const appPersistedReducer = (
126128
}
127129
}
128130

131+
case ActionTypes.SetWorkerRegistration: {
132+
return {
133+
...state,
134+
workerRegistration: action.payload.workerRegistration,
135+
}
136+
}
137+
129138
default:
130139
return state
131140
}

0 commit comments

Comments
 (0)