Skip to content

Commit f741f75

Browse files
author
Carlos Rufo
committed
update service worker
1 parent 9106dea commit f741f75

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed
Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,135 @@
1-
// In production, we register a service worker to serve assets from local cache.
1+
// This optional code is used to register a service worker.
2+
// register() is not called by default.
23

34
// This lets the app load faster on subsequent visits in production, and gives
45
// it offline capabilities. However, it also means that developers (and users)
5-
// will only see deployed updates on the "N+1" visit to a page, since previously
6-
// cached resources are updated in the background.
6+
// will only see deployed updates on subsequent visits to a page, after all the
7+
// existing tabs open on the page have been closed, since previously cached
8+
// resources are updated in the background.
79

8-
// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
9-
// This link also includes instructions on opting out of this behavior.
10+
// To learn more about the benefits of this model and instructions on how to
11+
// opt-in, read http://bit.ly/CRA-PWA
1012

1113
const isLocalhost = Boolean(
1214
window.location.hostname === 'localhost' ||
1315
// [::1] is the IPv6 localhost address.
1416
window.location.hostname === '[::1]' ||
1517
// 127.0.0.1/8 is considered localhost for IPv4.
1618
window.location.hostname.match(
17-
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
18-
)
19-
);
19+
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/,
20+
),
21+
)
2022

21-
export default function register() {
23+
export function register(config) {
2224
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
2325
// The URL constructor is available in all browsers that support SW.
24-
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
26+
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href)
2527
if (publicUrl.origin !== window.location.origin) {
2628
// Our service worker won't work if PUBLIC_URL is on a different origin
2729
// from what our page is served on. This might happen if a CDN is used to
28-
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
29-
return;
30+
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
31+
return
3032
}
3133

3234
window.addEventListener('load', () => {
33-
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
35+
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`
3436

3537
if (isLocalhost) {
36-
// This is running on localhost. Lets check if a service worker still exists or not.
37-
checkValidServiceWorker(swUrl);
38+
// This is running on localhost. Let's check if a service worker still exists or not.
39+
checkValidServiceWorker(swUrl, config)
3840

3941
// Add some additional logging to localhost, pointing developers to the
4042
// service worker/PWA documentation.
4143
navigator.serviceWorker.ready.then(() => {
4244
console.log(
4345
'This web app is being served cache-first by a service ' +
44-
'worker. To learn more, visit https://goo.gl/SC7cgQ'
45-
);
46-
});
46+
'worker. To learn more, visit http://bit.ly/CRA-PWA',
47+
)
48+
})
4749
} else {
48-
// Is not local host. Just register service worker
49-
registerValidSW(swUrl);
50+
// Is not localhost. Just register service worker
51+
registerValidSW(swUrl, config)
5052
}
51-
});
53+
})
5254
}
5355
}
5456

55-
function registerValidSW(swUrl) {
57+
function registerValidSW(swUrl, config) {
5658
navigator.serviceWorker
5759
.register(swUrl)
5860
.then(registration => {
5961
registration.onupdatefound = () => {
60-
const installingWorker = registration.installing;
62+
const installingWorker = registration.installing
63+
if (installingWorker == null) {
64+
return
65+
}
6166
installingWorker.onstatechange = () => {
6267
if (installingWorker.state === 'installed') {
6368
if (navigator.serviceWorker.controller) {
64-
// At this point, the old content will have been purged and
65-
// the fresh content will have been added to the cache.
66-
// It's the perfect time to display a "New content is
67-
// available; please refresh." message in your web app.
68-
console.log('New content is available; please refresh.');
69+
// At this point, the updated precached content has been fetched,
70+
// but the previous service worker will still serve the older
71+
// content until all client tabs are closed.
72+
console.log(
73+
'New content is available and will be used when all ' +
74+
'tabs for this page are closed. See http://bit.ly/CRA-PWA.',
75+
)
76+
77+
// Execute callback
78+
if (config && config.onUpdate) {
79+
config.onUpdate(registration)
80+
}
6981
} else {
7082
// At this point, everything has been precached.
7183
// It's the perfect time to display a
7284
// "Content is cached for offline use." message.
73-
console.log('Content is cached for offline use.');
85+
console.log('Content is cached for offline use.')
86+
87+
// Execute callback
88+
if (config && config.onSuccess) {
89+
config.onSuccess(registration)
90+
}
7491
}
7592
}
76-
};
77-
};
93+
}
94+
}
7895
})
7996
.catch(error => {
80-
console.error('Error during service worker registration:', error);
81-
});
97+
console.error('Error during service worker registration:', error)
98+
})
8299
}
83100

84-
function checkValidServiceWorker(swUrl) {
101+
function checkValidServiceWorker(swUrl, config) {
85102
// Check if the service worker can be found. If it can't reload the page.
86103
fetch(swUrl)
87104
.then(response => {
88105
// Ensure service worker exists, and that we really are getting a JS file.
106+
const contentType = response.headers.get('content-type')
89107
if (
90108
response.status === 404 ||
91-
response.headers.get('content-type').indexOf('javascript') === -1
109+
(contentType != null && contentType.indexOf('javascript') === -1)
92110
) {
93111
// No service worker found. Probably a different app. Reload the page.
94112
navigator.serviceWorker.ready.then(registration => {
95113
registration.unregister().then(() => {
96-
window.location.reload();
97-
});
98-
});
114+
window.location.reload()
115+
})
116+
})
99117
} else {
100118
// Service worker found. Proceed as normal.
101-
registerValidSW(swUrl);
119+
registerValidSW(swUrl, config)
102120
}
103121
})
104122
.catch(() => {
105123
console.log(
106-
'No internet connection found. App is running in offline mode.'
107-
);
108-
});
124+
'No internet connection found. App is running in offline mode.',
125+
)
126+
})
109127
}
110128

111129
export function unregister() {
112130
if ('serviceWorker' in navigator) {
113131
navigator.serviceWorker.ready.then(registration => {
114-
registration.unregister();
115-
});
132+
registration.unregister()
133+
})
116134
}
117135
}

0 commit comments

Comments
 (0)