Skip to content

Commit 28cf14b

Browse files
Added service worker and manifest.json for PWA capabilities (#17)
1 parent cce167a commit 28cf14b

File tree

10 files changed

+193
-1
lines changed

10 files changed

+193
-1
lines changed

public/icons/icon_128x128.png

6.74 KB
Loading

public/icons/icon_192x192.png

10.6 KB
Loading

public/icons/icon_256x256.png

20.6 KB
Loading

public/icons/icon_384x384.png

39.6 KB
Loading

public/icons/icon_512x512.png

62.6 KB
Loading

public/icons/icon_96x96.png

4.37 KB
Loading

public/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="apple-mobile-web-app-status-bar-style" content="default">
9+
<meta name="description" content="A sample weather app">
10+
<meta name="theme-color" content="#2F3BA2" />
11+
12+
<link rel="apple-touch-icon" href="/images/icons/icon-152x152.png">
513
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
614
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,600&display=swap" rel="stylesheet">
7-
<meta name="viewport" content="width=device-width, initial-scale=1" />
15+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
16+
817
<title>React App</title>
918
</head>
1019
<body>

public/manifest.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"icons": [
3+
{
4+
"src": "/icons/icon_512x512.png",
5+
"sizes": "512x512",
6+
"type": "image/png"
7+
},
8+
{
9+
"src": "/icons/icon_384x384.png",
10+
"sizes": "384x384",
11+
"type": "image/png"
12+
},
13+
{
14+
"src": "/icons/icon_256x256.png",
15+
"sizes": "256x256",
16+
"type": "image/png"
17+
},
18+
{
19+
"src": "/icons/icon_192x192.png",
20+
"sizes": "192x192",
21+
"type": "image/png"
22+
},
23+
{
24+
"src": "/icons/icon_128x128.png",
25+
"sizes": "128x128",
26+
"type": "image/png"
27+
},
28+
{
29+
"src": "/icons/icon_96x96.png",
30+
"sizes": "96x96",
31+
"type": "image/png"
32+
}
33+
],
34+
"name": "EPCC Reference Storefront",
35+
"short_name": "EP Commerce Cloud",
36+
"orientation": "portrait",
37+
"display": "standalone",
38+
"start_url": ".",
39+
"description": "EP Commerce Cloud Reference Storefront",
40+
"background_color": "#FFFFFF",
41+
"theme_color": "#000000"
42+
}

src/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import App from './App';
4+
import * as serviceWorker from './serviceWorker';
45
import './index.scss';
56

67
if (process.env.NODE_ENV !== 'production') {
@@ -10,3 +11,5 @@ if (process.env.NODE_ENV !== 'production') {
1011
}
1112

1213
ReactDOM.render(<App />, document.getElementById('root'));
14+
15+
serviceWorker.register();

src/serviceWorker.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
const isLocalhost = Boolean(
2+
window.location.hostname === 'localhost' ||
3+
// [::1] is the IPv6 localhost address.
4+
window.location.hostname === '[::1]' ||
5+
// 127.0.0.0/8 are considered localhost for IPv4.
6+
window.location.hostname.match(
7+
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
8+
)
9+
);
10+
11+
type Config = {
12+
onSuccess?: (registration: ServiceWorkerRegistration) => void;
13+
onUpdate?: (registration: ServiceWorkerRegistration) => void;
14+
};
15+
16+
export function register(config?: Config) {
17+
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
18+
// The URL constructor is available in all browsers that support SW.
19+
const publicUrl = new URL(
20+
process.env.PUBLIC_URL,
21+
window.location.href
22+
);
23+
if (publicUrl.origin !== window.location.origin) {
24+
// Our service worker won't work if PUBLIC_URL is on a different origin
25+
// from what our page is served on. This might happen if a CDN is used to
26+
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
27+
return;
28+
}
29+
30+
window.addEventListener('load', () => {
31+
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
32+
33+
if (isLocalhost) {
34+
// This is running on localhost. Let's check if a service worker still exists or not.
35+
checkValidServiceWorker(swUrl, config);
36+
37+
// Add some additional logging to localhost, pointing developers to the
38+
// service worker/PWA documentation.
39+
navigator.serviceWorker.ready.then(() => {
40+
console.log(
41+
'This web app is being served cache-first by a service ' +
42+
'worker. To learn more, visit https://bit.ly/CRA-PWA'
43+
);
44+
});
45+
} else {
46+
// Is not localhost. Just register service worker
47+
registerValidSW(swUrl, config);
48+
}
49+
});
50+
}
51+
}
52+
53+
function registerValidSW(swUrl: string, config?: Config) {
54+
navigator.serviceWorker
55+
.register(swUrl)
56+
.then(registration => {
57+
registration.onupdatefound = () => {
58+
const installingWorker = registration.installing;
59+
if (installingWorker == null) {
60+
return;
61+
}
62+
installingWorker.onstatechange = () => {
63+
if (installingWorker.state === 'installed') {
64+
if (navigator.serviceWorker.controller) {
65+
// At this point, the updated precached content has been fetched,
66+
// but the previous service worker will still serve the older
67+
// content until all client tabs are closed.
68+
console.log(
69+
'New content is available and will be used when all ' +
70+
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
71+
);
72+
73+
// Execute callback
74+
if (config && config.onUpdate) {
75+
config.onUpdate(registration);
76+
}
77+
console.warn(registration);
78+
} else {
79+
// At this point, everything has been precached.
80+
// It's the perfect time to display a
81+
// "Content is cached for offline use." message.
82+
console.log('Content is cached for offline use.');
83+
84+
// Execute callback
85+
if (config && config.onSuccess) {
86+
config.onSuccess(registration);
87+
}
88+
}
89+
}
90+
};
91+
};
92+
})
93+
.catch(error => {
94+
console.error('Error during service worker registration:', error);
95+
});
96+
}
97+
98+
function checkValidServiceWorker(swUrl: string, config?: Config) {
99+
// Check if the service worker can be found. If it can't reload the page.
100+
fetch(swUrl, {
101+
headers: { 'Service-Worker': 'script' }
102+
})
103+
.then(response => {
104+
// Ensure service worker exists, and that we really are getting a JS file.
105+
const contentType = response.headers.get('content-type');
106+
if (
107+
response.status === 404 ||
108+
(contentType != null && contentType.indexOf('javascript') === -1)
109+
) {
110+
// No service worker found. Probably a different app. Reload the page.
111+
navigator.serviceWorker.ready.then(registration => {
112+
registration.unregister().then(() => {
113+
window.location.reload();
114+
});
115+
});
116+
} else {
117+
// Service worker found. Proceed as normal.
118+
registerValidSW(swUrl, config);
119+
}
120+
})
121+
.catch(() => {
122+
console.log(
123+
'No internet connection found. App is running in offline mode.'
124+
);
125+
});
126+
}
127+
128+
export function unregister() {
129+
if ('serviceWorker' in navigator) {
130+
navigator.serviceWorker.ready
131+
.then(registration => {
132+
registration.unregister();
133+
})
134+
.catch(error => {
135+
console.error(error.message);
136+
});
137+
}
138+
}

0 commit comments

Comments
 (0)