Skip to content

Commit 26c50c2

Browse files
committed
added registersw.js test
1 parent 5488af9 commit 26c50c2

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/registerServiceWorker.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Leave this file alone, I'm just testing something, not part of the project.
2+
// You can delete this file if you like.
3+
const isLocalhost = Boolean(
4+
window.location.hostname === 'localhost' ||
5+
// [::1] is the IPv6 localhost address.
6+
window.location.hostname === '[::1]' ||
7+
// 127.0.0.1/8 is considered localhost for IPv4.
8+
window.location.hostname.match(
9+
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
10+
)
11+
);
12+
13+
export default function register() {
14+
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
15+
// The URL constructor is available in all browsers that support SW.
16+
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
17+
if (publicUrl.origin !== window.location.origin) {
18+
// Our service worker won't work if PUBLIC_URL is on a different origin
19+
// from what our page is served on. This might happen if a CDN is used to
20+
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
21+
return;
22+
}
23+
24+
window.addEventListener('load', () => {
25+
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
26+
27+
if (isLocalhost) {
28+
// This is running on localhost. Lets check if a service worker still exists or not.
29+
checkValidServiceWorker(swUrl);
30+
} else {
31+
// Is not local host. Just register service worker
32+
registerValidSW(swUrl);
33+
}
34+
});
35+
}
36+
}
37+
38+
function registerValidSW(swUrl) {
39+
navigator.serviceWorker
40+
.register(swUrl)
41+
.then(registration => {
42+
registration.onupdatefound = () => {
43+
const installingWorker = registration.installing;
44+
installingWorker.onstatechange = () => {
45+
if (installingWorker.state === 'installed') {
46+
if (navigator.serviceWorker.controller) {
47+
// At this point, the old content will have been purged and
48+
// the fresh content will have been added to the cache.
49+
// It's the perfect time to display a "New content is
50+
// available; please refresh." message in your web app.
51+
console.log('New content is available; please refresh.');
52+
} else {
53+
// At this point, everything has been precached.
54+
// It's the perfect time to display a
55+
// "Content is cached for offline use." message.
56+
console.log('Content is cached for offline use.');
57+
}
58+
}
59+
};
60+
};
61+
})
62+
.catch(error => {
63+
console.error('Error during service worker registration:', error);
64+
});
65+
}
66+
67+
function checkValidServiceWorker(swUrl) {
68+
// Check if the service worker can be found. If it can't reload the page.
69+
fetch(swUrl)
70+
.then(response => {
71+
// Ensure service worker exists, and that we really are getting a JS file.
72+
if (
73+
response.status === 404 ||
74+
response.headers.get('content-type').indexOf('javascript') === -1
75+
) {
76+
// No service worker found. Probably a different app. Reload the page.
77+
navigator.serviceWorker.ready.then(registration => {
78+
registration.unregister().then(() => {
79+
window.location.reload();
80+
});
81+
});
82+
} else {
83+
// Service worker found. Proceed as normal.
84+
registerValidSW(swUrl);
85+
}
86+
})
87+
.catch(() => {
88+
console.log(
89+
'No internet connection found. App is running in offline mode.'
90+
);
91+
});
92+
}
93+
94+
export function unregister() {
95+
if ('serviceWorker' in navigator) {
96+
navigator.serviceWorker.ready.then(registration => {
97+
registration.unregister();
98+
});
99+
}
100+
}

0 commit comments

Comments
 (0)