Skip to content

Commit 9e8e995

Browse files
committed
Fix types in default service worker file
1 parent 88e7d42 commit 9e8e995

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/react-scripts/template/src/serviceWorker.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ type Config = {
2828
export function register(config: Config) {
2929
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
3030
// 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+
);
3235
if (publicUrl.origin !== window.location.origin) {
3336
// Our service worker won't work if PUBLIC_URL is on a different origin
3437
// 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) {
5962
}
6063
}
6164

62-
function registerValidSW(swUrl, config) {
65+
function registerValidSW(swUrl: string, config: Config) {
6366
navigator.serviceWorker
6467
.register(swUrl)
6568
.then(registration => {
6669
registration.onupdatefound = () => {
6770
const installingWorker = registration.installing;
71+
if (installingWorker == null) {
72+
return;
73+
}
6874
installingWorker.onstatechange = () => {
6975
if (installingWorker.state === 'installed') {
7076
if (navigator.serviceWorker.controller) {
@@ -100,14 +106,15 @@ function registerValidSW(swUrl, config) {
100106
});
101107
}
102108

103-
function checkValidServiceWorker(swUrl, config) {
109+
function checkValidServiceWorker(swUrl: string, config: Config) {
104110
// Check if the service worker can be found. If it can't reload the page.
105111
fetch(swUrl)
106112
.then(response => {
107113
// Ensure service worker exists, and that we really are getting a JS file.
114+
const contentType = response.headers.get('content-type');
108115
if (
109116
response.status === 404 ||
110-
response.headers.get('content-type').indexOf('javascript') === -1
117+
(contentType != null && contentType.indexOf('javascript') === -1)
111118
) {
112119
// No service worker found. Probably a different app. Reload the page.
113120
navigator.serviceWorker.ready.then(registration => {

0 commit comments

Comments
 (0)