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": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "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 ./", "build": "tsc --project ./",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },

View file

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

View file

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