Fix service worker placement

This commit is contained in:
William Brawner 2022-02-04 21:16:26 -07:00
parent 717ddb3884
commit adcf571be4
3 changed files with 4 additions and 7 deletions

View file

@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"prebuild": "copyfiles -Ve src/** index.html manifest.json **/*.css **/*.png ../dist/public",
"prebuild": "copyfiles -Ve src/** index.html manifest.json sw.js **/*.css **/*.png ../dist/public",
"build": "tsc --project ./",
"test": "echo \"Error: no test specified\" && exit 1"
},

View file

@ -1,7 +1,7 @@
let socket: WebSocket | null = null;
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/static/js/sw.js', { 'scope': '/' })
navigator.serviceWorker.register('/static/js/sw.js')
.then(function (registration) {
console.log('Registration successful, scope is:', registration.scope);
})

View file

@ -22,10 +22,7 @@ const filesToCache = [
const staticCacheName = 'pages-cache-v1';
// TODO: Import this properly
type InstallEvent = any;
self.addEventListener('install', (event: InstallEvent) => {
self.addEventListener('install', (event) => {
console.log('Attempting to install service worker and cache static assets');
event.waitUntil(
caches.open(staticCacheName)
@ -35,7 +32,7 @@ self.addEventListener('install', (event: InstallEvent) => {
);
});
self.addEventListener('fetch', (event: InstallEvent) => {
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
caches.open(staticCacheName).then((cache) => {