@@ -28,7 +28,10 @@ type Config = {
28
28
export function register ( config : Config ) {
29
29
if ( process . env . NODE_ENV === 'production' && 'serviceWorker' in navigator ) {
30
30
// The URL constructor is available in all browsers that support SW.
31
- const publicUrl = new URL ( process . env . PUBLIC_URL , window . location . href ) ;
31
+ const publicUrl = new URL (
32
+ ( process as { env : { [ key : string ] : string } } ) . env . PUBLIC_URL ,
33
+ window . location . href
34
+ ) ;
32
35
if ( publicUrl . origin !== window . location . origin ) {
33
36
// Our service worker won't work if PUBLIC_URL is on a different origin
34
37
// from what our page is served on. This might happen if a CDN is used to
@@ -59,12 +62,15 @@ export function register(config: Config) {
59
62
}
60
63
}
61
64
62
- function registerValidSW ( swUrl , config ) {
65
+ function registerValidSW ( swUrl : string , config : Config ) {
63
66
navigator . serviceWorker
64
67
. register ( swUrl )
65
68
. then ( registration => {
66
69
registration . onupdatefound = ( ) => {
67
70
const installingWorker = registration . installing ;
71
+ if ( installingWorker == null ) {
72
+ return ;
73
+ }
68
74
installingWorker . onstatechange = ( ) => {
69
75
if ( installingWorker . state === 'installed' ) {
70
76
if ( navigator . serviceWorker . controller ) {
@@ -100,14 +106,15 @@ function registerValidSW(swUrl, config) {
100
106
} ) ;
101
107
}
102
108
103
- function checkValidServiceWorker ( swUrl , config ) {
109
+ function checkValidServiceWorker ( swUrl : string , config : Config ) {
104
110
// Check if the service worker can be found. If it can't reload the page.
105
111
fetch ( swUrl )
106
112
. then ( response => {
107
113
// Ensure service worker exists, and that we really are getting a JS file.
114
+ const contentType = response . headers . get ( 'content-type' ) ;
108
115
if (
109
116
response . status === 404 ||
110
- response . headers . get ( 'content-type' ) . indexOf ( 'javascript' ) === - 1
117
+ ( contentType != null && contentType . indexOf ( 'javascript' ) === - 1 )
111
118
) {
112
119
// No service worker found. Probably a different app. Reload the page.
113
120
navigator . serviceWorker . ready . then ( registration => {
0 commit comments