Skip to content

Commit

Permalink
PWA init, some bug #6
Browse files Browse the repository at this point in the history
  • Loading branch information
harasees-singh committed Sep 5, 2022
1 parent b35348b commit 77731d7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ class MyDocument extends Document {
render() {
return (
<Html lang='en'>
<head>
{/* <script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.getRegistrations()
.then((res) => {
if (res.length > 0) {
res?.[0]?.unregister?.()
}
window.addEventListener('load', () => {
return navigator.serviceWorker
.register('./serviceWorker.js')
.then((reg) => null)
.catch((error) => null)
})
})
.catch(() => null)
}
</script> */}
</head>
<Head>
<meta
name='description'
Expand Down
35 changes: 35 additions & 0 deletions public/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const CACHE_NAME = 'version-1'
const urlsToCache = ['index.html', 'offline.html']

const self = this

self.addEventListener('install', (evt) => {
evt.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(urlsToCache))
)
})

self.addEventListener('fetch', (evt) => {
evt.respondWith(
caches
.match(evt.request)
.then(() => fetch(evt.request).catch(() => caches.match('offline.html')))
)
})

self.addEventListener('activate', (evt) => {
const cacheWhiteList = []
cacheWhiteList.push(CACHE_NAME)
evt.waitUntil(
caches.keys().then((cacheNames) =>
Promise.all(
cacheNames.map((item) => {
if (!cacheWhiteList.includes(item)) {
return caches.delete(item)
}
return item
})
)
)
)
})

1 comment on commit 77731d7

@vercel
Copy link

@vercel vercel bot commented on 77731d7 Sep 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

coderecs – ./

coderecs-git-main-pikachu.vercel.app
coderecs-pikachu.vercel.app
coderecs.vercel.app

Please sign in to comment.