Skip to content

Commit 2db970d

Browse files
authored
fix(EAR-3809): explicitly set service worker scope (#6377)
* fix(EAR-3809): explicitly set service worker scope, to be the root origin instead of the static nested scope. * fix(EAR-3809): make scope as narrow as possible, and claim() with the newest worker loaded, in order to debug failing tests. * fix(EAR-3809): browser security has scoping rules that can only be narrowed, not broadened. Therefore the bundle which loads the worker, and the worker itself, has to not be within the static scope.
1 parent 024f671 commit 2db970d

File tree

7 files changed

+37
-6
lines changed

7 files changed

+37
-6
lines changed

src/shared/components/CSVExportButton.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ class CSVExportButton extends PureComponent<Props, State> {
2525

2626
if (props.workerRegistration) {
2727
;(props.workerRegistration as Promise<ServiceWorkerRegistration>).then(
28-
() => this.setState({browserSupportsDownload: true}),
28+
registrationResult => {
29+
if (registrationResult.active.state == 'activated') {
30+
this.setState({browserSupportsDownload: true})
31+
} else {
32+
console.error(
33+
'Feature not available, because ServiceWorker is registered but inactive.'
34+
)
35+
}
36+
},
2937
function (err) {
3038
console.error(
3139
'Feature not available, because ServiceWorker registration failed: ',

src/shared/contexts/app.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ import {presentationMode as presentationModeCopy} from 'src/shared/copy/notifica
2828
import {AppState, TimeZone, Theme, NavBarState, FlowsCTA} from 'src/types'
2929
import {event} from 'src/cloud/utils/reporting'
3030

31-
import {registerServiceWorker} from 'src/shared/workers/serviceWorker'
32-
33-
const workerRegistration = registerServiceWorker()
31+
let workerRegistration
32+
import(
33+
/* webpackPreload: true */
34+
/* webpackChunkName: "setup-interceptor" */
35+
'src/shared/workers/serviceWorker'
36+
).then(
37+
({registerServiceWorker}) => (workerRegistration = registerServiceWorker())
38+
)
3439

3540
interface AppSettingContextType {
3641
timeZone: TimeZone

src/shared/workers/downloadHelper.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ self.addEventListener('fetch', function (event: any) {
6767
event.respondWith(fetch(event.request))
6868
}
6969
})
70+
71+
/// Have the latest version register (including updated registration settings).
72+
self.addEventListener('activate', event => {
73+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
74+
// @ts-ignore
75+
event.waitUntil(clients.claim())
76+
})

src/shared/workers/serviceWorker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export const registerServiceWorker = (): Promise<ServiceWorkerRegistration> => {
66
workerRegistration = navigator.serviceWorker.register(
77
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
88
// @ts-ignore
9-
new URL('../workers/downloadHelper.ts', import.meta.url)
9+
new URL('../workers/downloadHelper.ts', import.meta.url),
10+
{scope: '/api/v2/query'}
11+
/* webpackChunkName: "interceptor" */
1012
)
1113
}
1214

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"noImplicitUseStrict": false,
1515
"noUnusedParameters": true,
1616
"noUnusedLocals": true,
17-
"removeComments": true,
1817
"importHelpers": true,
1918
"noLib": false,
2019
"noEmitHelpers": true,

webpack.fast.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ module.exports = merge(common, {
77
mode: 'none',
88
output: {
99
filename: `${STATIC_DIR}[contenthash:10].js`,
10+
chunkFilename: pathData => {
11+
return ['interceptor', 'setup-interceptor'].includes(pathData.chunk.name)
12+
? '[contenthash:10].js'
13+
: `${STATIC_DIR}[contenthash:10].js`
14+
},
1015
},
1116
})

webpack.prod.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ module.exports = mergeWebpack(commonWebpack, {
1515
devtool: 'source-map',
1616
output: {
1717
filename: `${DIRECTORY_STATIC}[contenthash:10].js`,
18+
chunkFilename: pathData => {
19+
return ['interceptor', 'setup-interceptor'].includes(pathData.chunk.name)
20+
? '[contenthash:10].js'
21+
: `${DIRECTORY_STATIC}[contenthash:10].js`
22+
},
1823
},
1924
module: {
2025
rules: [

0 commit comments

Comments
 (0)