Skip to content

Commit 08675bd

Browse files
committed
refactor: move service worker to src
1 parent c71d0db commit 08675bd

3 files changed

Lines changed: 29 additions & 44 deletions

File tree

programmerbar-web/src/lib/utils/push-notifications.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
1-
/**
2-
* Utility functions for managing push notifications
3-
*/
4-
5-
/**
6-
* Register service worker
7-
*/
8-
export async function registerServiceWorker(): Promise<ServiceWorkerRegistration | null> {
9-
if (!('serviceWorker' in navigator)) {
10-
console.warn('Service workers are not supported in this browser');
11-
return null;
12-
}
13-
14-
try {
15-
const registration = await navigator.serviceWorker.register('/service-worker.js', {
16-
scope: '/'
17-
});
18-
console.log('Service Worker registered successfully:', registration);
19-
return registration;
20-
} catch (error) {
21-
console.error('Service Worker registration failed:', error);
22-
return null;
23-
}
24-
}
25-
261
/**
272
* Check if push notifications are supported
283
*/

programmerbar-web/src/routes/+layout.svelte

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
44
import { setUserContext } from '$lib/states/user.svelte';
55
import { Toaster } from 'svelte-sonner';
6-
import { registerServiceWorker } from '$lib/utils/push-notifications';
7-
import { onMount } from 'svelte';
8-
import { browser } from '$app/environment';
96
107
const { data, children } = $props();
118
@@ -18,13 +15,6 @@
1815
});
1916
2017
setUserContext(userState);
21-
22-
// Register service worker for push notifications
23-
onMount(() => {
24-
if (browser) {
25-
registerServiceWorker();
26-
}
27-
});
2818
</script>
2919

3020
<Toaster richColors />

programmerbar-web/static/service-worker.js renamed to programmerbar-web/src/service-worker.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
// Service Worker for handling push notifications
1+
/// <reference no-default-lib="true"/>
2+
/// <reference lib="esnext" />
3+
/// <reference lib="webworker" />
4+
/// <reference types="@sveltejs/kit" />
25

3-
const CACHE_NAME = 'programmerbar-v1';
6+
import { build, files, version } from '$service-worker';
47

5-
self.addEventListener('install', () => {
6-
console.log('[Service Worker] Installing...');
7-
self.skipWaiting();
8+
const self = globalThis.self as unknown as ServiceWorkerGlobalScope;
9+
10+
const CACHE = `cache-${version}`;
11+
const ASSETS = [...build, ...files];
12+
13+
self.addEventListener('install', (event) => {
14+
console.log('[Service Worker] Install event');
15+
16+
async function addFilesToCache() {
17+
const cache = await caches.open(CACHE);
18+
await cache.addAll(ASSETS);
19+
}
20+
21+
event.waitUntil(addFilesToCache());
822
});
923

10-
// Activate event
1124
self.addEventListener('activate', (event) => {
12-
console.log('[Service Worker] Activating...');
13-
event.waitUntil(self.clients.claim());
25+
console.log('[Service Worker] Activate event');
26+
27+
async function deleteOldCaches() {
28+
for (const key of await caches.keys()) {
29+
if (key !== CACHE) await caches.delete(key);
30+
}
31+
}
32+
33+
event.waitUntil(deleteOldCaches());
1434
});
1535

1636
// Push event -> show notification
@@ -80,7 +100,7 @@ self.addEventListener('pushsubscriptionchange', (event) => {
80100
console.log('[Service Worker] Push subscription changed');
81101

82102
event.waitUntil(
83-
self.registration.pushManager.subscribe(event.oldSubscription.options).then((subscription) => {
103+
self.registration.pushManager.subscribe(event.oldSubscription?.options).then((subscription) => {
84104
console.log('[Service Worker] Resubscribed:', subscription);
85105
return fetch('/api/push/subscribe', {
86106
method: 'POST',

0 commit comments

Comments
 (0)