Skip to content

Commit 41c1a9b

Browse files
committed
Add service-worker
1 parent 607a8a3 commit 41c1a9b

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

public/index.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<meta name="theme-color" content="#00d1b2">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
77
<link rel="manifest" href="/manifest.json">
8-
<link rel="apple-touch-icon" sizes="180x180" href="/favicons/apple-touch-icon.png">
9-
<link rel="icon" type="image/png" href="/favicons/favicon-32x32.png" sizes="32x32">
10-
<link rel="icon" type="image/png" href="/favicons/favicon-16x16.png" sizes="16x16">
11-
<link rel="manifest" href="/manifest.json">
12-
<link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#5bbad5">
8+
<link rel="apple-touch-icon" sizes="180x180" href="favicons/apple-touch-icon.png">
9+
<link rel="icon" type="image/png" href="favicons/favicon-32x32.png" sizes="32x32">
10+
<link rel="icon" type="image/png" href="favicons/favicon-16x16.png" sizes="16x16">
11+
<link rel="manifest" href="manifest.json">
12+
<link rel="mask-icon" href="favicons/safari-pinned-tab.svg" color="#5bbad5">
1313
<title>React Client</title>
1414
</head>
1515
<body>
@@ -18,6 +18,20 @@
1818
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/2.8.1/ReactRouter.min.js" integrity="sha256-Ve10xzUoKZr2up5joKtdW0RfY6R/6nWBO0x8AuOxp1s=" crossorigin="anonymous"></script>
1919
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js" integrity="sha256-JIW8lNqN2EtqC6ggNZYnAdKMJXRQfkPMvdRt+b0/Jxc=" crossorigin="anonymous"></script>
2020
<script async src="dist/bundle.min.js"></script>
21+
<script>
22+
// Check for browser support of service worker
23+
if ('serviceWorker' in navigator) {
24+
navigator.serviceWorker.register('service-worker.js')
25+
.then(function(registration) {
26+
// Successful registration
27+
console.log('Hooray. Registration successful, scope is:', registration.scope);
28+
})
29+
.catch(function(error) {
30+
// Failed registration, service worker won’t be installed
31+
console.log('Whoops. Service worker registration failed, error:', error);
32+
});
33+
}
34+
</script>
2135
<noscript>
2236
<p>Ops! JavaScript is disabled. You can't use this app.</p>
2337
</noscript>

public/service-worker.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var CACHE_NAME = 'v1::iClient';
2+
var urlsToCache = [
3+
'/',
4+
'/index.html',
5+
'/dist/bundle.min.js',
6+
'/favicons/favicon-16x16.png',
7+
'https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-with-addons.min.js',
8+
'https://cdnjs.cloudflare.com/ajax/libs/react-router/2.8.1/ReactRouter.min.js',
9+
'https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js',
10+
];
11+
12+
self.addEventListener('install', function(event) {
13+
event.waitUntil(
14+
caches.open(CACHE_NAME).then(function(cache) {
15+
return cache.addAll(urlsToCache);
16+
})
17+
);
18+
});
19+
20+
self.addEventListener('activate', function(event) {
21+
event.waitUntil(
22+
caches.keys().then(function(cacheNames) {
23+
return Promise.all(
24+
cacheNames.filter(function(cacheName) {
25+
return cacheName !== CACHE_NAME;
26+
}).map(function(cacheName) {
27+
return caches.delete(cacheName);
28+
})
29+
);
30+
})
31+
);
32+
});
33+
self.addEventListener('fetch', function(event) {
34+
var requestURL = new URL(event.request.url);
35+
36+
event.respondWith(
37+
caches.open(CACHE_NAME).then(function(cache) {
38+
return cache.match(event.request).then(function(response) {
39+
if (response) {
40+
return response;
41+
}
42+
return fetch(event.request).then(function(response) {
43+
if (response.ok) {
44+
cache.put(event.request, response.clone()).catch(function(error) {
45+
return new Response("Request failed!");
46+
});
47+
}
48+
return response;
49+
}).catch(function(e) {
50+
return new Response("Request failed!");
51+
});
52+
});
53+
})
54+
);
55+
});
56+

0 commit comments

Comments
 (0)