Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pwa #35

Merged
merged 18 commits into from
Dec 8, 2019
Merged

Pwa #35

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added client/public/developer-icon-144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/developer-icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/developer-icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified client/public/favicon.ico
Binary file not shown.
23 changes: 22 additions & 1 deletion client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<!-- Safari compatilibility -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="apple-mobile-web-app-title" content="Developer Hub" />
<link rel="apple-touch-icon" href="developer-icon-144.png" />
<!-- IE compatilibility -->
<meta name="msapplication-TileImage" content="developer-icon-144.png" />
<meta name="msapplication-TileColor" content="#fff" />
<meta name="theme-color" content="#3f51b5" />
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
Expand All @@ -21,11 +30,23 @@
rel="stylesheet"
/>
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>Welcome To DevConnector</title>
<title>Hack Your Social</title>
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="text/javascript">
window.addEventListener('load', async event => {
if ('serviceWorker' in navigator) {
try {
await navigator.serviceWorker.register('./sw.js');
console.log('Service worker registered!');
} catch (error) {
console.error('Service worker failed!');
}
}
});
</script>
</body>
</html>
27 changes: 22 additions & 5 deletions client/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"short_name": "Hack Your Social",
"name": "Hack Your Social",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "developer-icon-144.png",
"type": "image/png",
"sizes": "144x144"
},
{
"src": "developer-icon-192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "developer-icon-512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"start_url": "./index.html",
"scope": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
"theme_color": "#3f51b5",
"background_color": "#ffffff",
"orientation": "portrait-primary"
}
32 changes: 32 additions & 0 deletions client/public/offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<!-- Safari compatilibility -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="apple-mobile-web-app-title" content="Developer Hub" />
<link rel="apple-touch-icon" href="developer-icon-144.png" />
<!-- IE compatilibility -->
<meta name="msapplication-TileImage" content="developer-icon-144.png" />
<meta name="msapplication-TileColor" content="#fff" />
<meta name="theme-color" content="#3f51b5" />
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf"
crossorigin="anonymous"
/>
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>Hack Your Social</title>
</head>
<body>
<p className="lead"><i className="fas fa-error" /> We are not able to cache this page yet!</p>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="swReg.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions client/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
128 changes: 128 additions & 0 deletions client/public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* eslint-disable no-restricted-globals */
const CACHE_VERSION = 1;
const CURRENT_CACHES = {
static: `static-v${CACHE_VERSION}`,
dynamic: `dynamic-v${CACHE_VERSION}`,
};
const STATIC_FILES = [
'/',
'/index.html',
'/offline.html',
'/static/media/showcase.4b31330b.jpg',
'/static/js/bundle.js',
'/static/js/0.chunk.js',
'/static/js/main.chunk.js',
'/static/js/main.chunk.js.map',
'/manifest.json',
'/developer-icon-144.png',
'/sw.js',
'/favicon.ico',
'https://use.fontawesome.com/releases/v5.8.1/css/all.css',
'https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-solid-900.ttf',
'https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-solid-900.woff',
'https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-solid-900.woff2',
'https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-brands-400.ttf',
'https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-brands-400.woff',
'https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-brands-400.woff2',
];

const trimCache = (cacheName, maxItems) => {
caches.open(cacheName).then(cache => {
return cache.keys().then(keys => {
if (keys.length > maxItems) {
cache.delete(keys[0]).then(trimCache(cacheName, maxItems));
}
});
});
};

self.addEventListener('install', event => {
console.log('[SW] Installing sw', event);

event.waitUntil(
caches.open(CURRENT_CACHES.static).then(cache => {
console.log('Pre-caching...');
// Static Precaching
cache.addAll(STATIC_FILES);
}),
);
});

self.addEventListener('activate', event => {
console.log('[SW] Activating sw', event);
event.waitUntil(
caches.keys().then(keys => {
// Deleting obsolete caches
return Promise.all(
keys
.filter(key => key !== CURRENT_CACHES.static && key !== CURRENT_CACHES.dynamic)
.map(key => {
return caches.delete(key);
}),
);
}),
);
return self.clients.claim();
});

const isStaticAsset = (requestUrl, array) => {
const cachePath =
requestUrl.indexOf(self.origin) === 0 ? requestUrl.substring(self.origin.length) : requestUrl;
return array.indexOf(cachePath) > -1;
};

const apis = [`${self.location.origin}/api/profile`];

// Cache then network with fallback & dynamic caching
self.addEventListener('fetch', event => {
if (apis.includes(event.request.url)) {
event.respondWith(
caches.open(CURRENT_CACHES.dynamic).then(cache => {
return fetch(event.request).then(response => {
const cloneResponse = response.clone();
trimCache(CURRENT_CACHES.dynamic, 30);
cache.put(event.request.url, cloneResponse);
return response;
});
}),
);
} else if (isStaticAsset(event.request.url, STATIC_FILES)) {
event.respondWith(caches.match(event.request));
} else {
event.respondWith(
caches.match(event.request).then(response => {
return (
response ||
// Dynamic Caching
fetch(event.request)
.then(response => {
return caches.open(CURRENT_CACHES.dynamic).then(cache => {
const cloneResponse = response.clone();
trimCache(CURRENT_CACHES.dynamic, 30);
cache.put(event.request.url, cloneResponse);
return response;
});
})
.catch(error => {
return caches.open(CURRENT_CACHES.static).then(cache => {
if (event.request.headers.get('accept').includes('text/html')) {
return cache.match('/offline.html');
}
});
})
);
}),
);
}
});

// Network with cache fallback
// self.addEventListener('fetch', event => {
// event.respondWith(fetch(event.request).catch(error => caches.match(event.request)));
// });

// Cache-only
// self.addEventListener('fetch', event => event.respondWith(caches.match(event.request)));

// Network-only
// self.addEventListener('fetch', event => event.respondWith(fetch(event.request)));
Loading