Skip to content

Commit

Permalink
Merge pull request #27 from chinmaym07/pwa
Browse files Browse the repository at this point in the history
Made it PWA optimized
  • Loading branch information
iharshit009 authored Dec 20, 2020
2 parents b70cc68 + 6778f02 commit a76c60e
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,6 @@ dmypy.json
.pytype/

# profiling data
.prof
.prof
# Local Netlify folder
.netlify
9 changes: 9 additions & 0 deletions browserconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square150x150logo src="/mstile-150x150.png"/>
<TileColor>#00aba9</TileColor>
</tile>
</msapplication>
</browserconfig>
78 changes: 77 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Secret Peek</title>

<meta name="description" content="Confession's Page">
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<link href="https://fonts.googleapis.com/css?family=Dosis:400,700" rel="stylesheet" />
<link href="style.css" rel="stylesheet" />
<link rel="shortcut icon" href="" />
<meta name="theme-color" content="#4dbaa8"/>
<link rel="manifest" href="manifest.json">
<link rel="apple-touch-icon" sizes="180x180" href="public/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="public/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="public/favicon-16x16.png">

<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#00aba9" />


</head>

<body>
Expand All @@ -27,6 +38,71 @@ <h1>Write your confession here</h1>
</div>

<script src="scripts.js"></script>
<script>
if('serviceWorker' in navigator) {

/**
* Define if <link rel='next|prev|prefetch'> should
* be preloaded when accessing this page
*/
const PREFETCH = true;

/**
* Define which link-rel's should be preloaded if enabled.
*/
const PREFETCH_LINK_RELS = ['index','next', 'prev', 'prefetch'];

/**
* prefetchCache
*/
function prefetchCache() {
if(navigator.serviceWorker.controller) {

let links = document.querySelectorAll(
PREFETCH_LINK_RELS.map((rel) => {
return 'link[rel='+rel+']';
}).join(',')
);

if(links.length > 0) {
Array.from(links)
.map((link) => {
let href = link.getAttribute('href');
navigator.serviceWorker.controller.postMessage({
action : 'cache',
url : href,
});
});
}


}
}

/**
* Register Service Worker
*/
navigator.serviceWorker
.register('/service-worker.js', { scope: '/' })
.then(() => {
console.log('Service Worker Registered');
});

/**
* Wait if ServiceWorker is ready
*/
navigator.serviceWorker
.ready
.then(() => {
if(PREFETCH) {
prefetchCache();
}
});

}


</script>
</body>

</html>
26 changes: 26 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "SecretPeek",
"short_name": "SecretPeek",
"icons": [
{
"src": "/public/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/public/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "/public/maskable_icon.png",
"sizes": "196x196",
"type": "image/png",
"purpose": "any maskable"
}
],
"start_url": "/",
"theme_color": "#000000",
"background_color": "#ffffff",
"display": "standalone"
}
Binary file added public/android-chrome-192x192.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 public/android-chrome-512x512.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 public/apple-touch-icon.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 public/favicon-16x16.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 public/favicon-32x32.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 public/favicon.ico
Binary file not shown.
Binary file added public/maskable_icon.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 public/mstile-150x150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions public/safari-pinned-tab.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
var CACHE_NAME = 'my-site-cache-v1';
const CACHE_VERSION = 2.3;

const BASE_CACHE_FILES = [
'/style.css',
'/script.js',
'/index.html',
'/manifest.json',
'/public',
];
const OFFLINE_CACHE_FILES = [
'/style.css',
'/script.js',
'/index.html',
];

const MAX_TTL = {
'/': 3600,
html: 43200,
json: 43200,
js: 86400,
css: 86400,
};

function installServiceWorker() {
return Promise.all(
[caches.open(CACHE_VERSIONS.assets).then((cache) => {
return cache.addAll(BASE_CACHE_FILES);
}
, err => console.error(`Error with ${CACHE_VERSIONS.assets}`, err)),
caches.open(CACHE_VERSIONS.offline).then((cache) => {
return cache.addAll(OFFLINE_CACHE_FILES);
}
, err => console.error(`Error with ${CACHE_VERSIONS.offline}`, err))]
)
.then(() => {
return self.skipWaiting();
}, err => console.error("Error with installation: ", err));
}



self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
// Cache hit - return response
if (response) {
return response;
}

return fetch(event.request).then(
function(response) {
// Check if we received a valid response
if(!response || response.status !== 200 || response.type !== 'basic') {
return response;
}

// IMPORTANT: Clone the response. A response is a stream
// and because we want the browser to consume the response
// as well as the cache consuming the response, we need
// to clone it so we have two streams.
var responseToCache = response.clone();

caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request, responseToCache);
});

return response;
}
);
})
);
});

0 comments on commit a76c60e

Please sign in to comment.