Skip to content

Commit

Permalink
pwa work (publiclab#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh570 authored and jywarren committed Aug 20, 2019
1 parent bc71c05 commit 5ddba29
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/embed.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<script src="../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="../node_modules/urlhash/urlHash.js"></script>
<script src="../dist/community-toolbox.js" charset="utf-8"></script>
<link rel="manifest" href="../manifest.json">

</head>

Expand Down
Binary file added images/icons/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.
14 changes: 12 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<!-- For supporting different browsers -->
<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="Commmunity Toolbox">
<!-- <link rel="apple-touch-icon" href="./images/icons/icon-144-dim.png"> -->
<meta name="msapplication-TileImage" content="./images/icons/icon-144.png">
<meta name="msapplication-TileColor" content="#fff">
<meta name="theme-color" content="#3f51b5">

<title>Community toolbox</title>
<link rel="shortcut icon" id="favicon" type="image/png" href="https://i.publiclab.org/system/images/photos/000/000/354/medium/Boots-ground-02.png"/>
<link rel="stylesheet" type="text/css" href="./node_modules/node-snackbar/dist/snackbar.min.css" />

<link rel="manifest" href="./manifest.json">
<script src="./src/scripts/pwainit.js"></script>


<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
Expand All @@ -33,7 +43,7 @@
<link rel="stylesheet" href="examples/demo.css">
<link rel="stylesheet" href="examples/themes.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.2/gh-fork-ribbon.min.css" />
<a id="forkMe-ribbon" class="github-fork-ribbon" href="https://github.com/publiclab/community-toolbox" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
Expand Down
20 changes: 20 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Community Toolbox",
"short_name": "Community Toolbox",
"start_url": "/index.html",
"scope": ".",
"display": "standalone",
"background_color": "#fff",
"theme_color": "#3F51B5",
"description": "A platform for community growth.",
"dir": "ltr",
"lang": "en-US",
"orientation": "portrait-primary",
"icons": [
{
"src": "./images/icons/icon-144.png",
"type": "image/png",
"sizes": "144x144"
}
]
}
12 changes: 12 additions & 0 deletions offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Community Toolbox</title>
</head>
<body>
<h1 style="text-align: center;">Sorry, You're offline!!!</h1>
</body>
</html>
9 changes: 9 additions & 0 deletions src/scripts/pwainit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var deferredPrompt;

if('serviceWorker' in navigator) {
navigator.serviceWorker
.register('../../sw.js')
.then(() => {
console.log("Service Worker registered!");
})
}
51 changes: 51 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var STATIC_CACHE = "static-v1";
var DYNAMIC_CACHE = "dynamic-v1";

self.addEventListener('install', (e) => {
console.log("[Service Worker] installing...");
});

self.addEventListener('activate', (e) => {
console.log("[Service Worker] activating...",e);
e.waitUntil(
caches.keys()
.then((keyList) => {
return Promise.all(keyList.map((key) => {
if(key !== STATIC_CACHE && key !== DYNAMIC_CACHE) {
return caches.delete(key);
}
}))
})
);

return self.clients.claim();
});


self.addEventListener('fetch', function (e) {
let urlReq = e.request;

e.respondWith(
fetch(urlReq)
.then((res) => {
var resClone = res.clone();
return caches.open(DYNAMIC_CACHE)
.then((cache) => {
cache.delete(urlReq.url)
.then((bool) => {
cache.add(urlReq.url, resClone);
})

return res;
})
})
.catch((err) => {
return caches.match(urlReq.url)
.then((data) => {
if(data!=undefined) {
return data;
}
})
})
);
});

0 comments on commit 5ddba29

Please sign in to comment.