Skip to content

Commit

Permalink
SW add type support
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Oct 26, 2023
1 parent 167826a commit 90163ba
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions packages/webapp/public/sw.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
// https://github.com/microsoft/TypeScript/issues/14877
// https://github.com/microsoft/TypeScript/issues/11781
/// <reference lib="webworker" />

/**
* @type {ServiceWorkerGlobalScope}
*/
const sw = self;

const cacheName = 'console';
const assetsToCache = [...new Set([self.location.pathname, '/'])].map((path) => self.location.origin + path);
const assetsToCache = [...new Set([sw.location.pathname, '/'])].map((path) => sw.location.origin + path);

self.addEventListener('activate', (event) => {
event.waitUntil(self.registration.navigationPreload?.enable() || Promise.resolve());
sw.addEventListener('activate', (event) => {
event.waitUntil(sw.registration.navigationPreload?.enable() || Promise.resolve());
});

self.addEventListener('install', () => {
self.skipWaiting();
self.caches.open(cacheName).then((cache) => {
sw.addEventListener('install', () => {
sw.skipWaiting();
sw.caches.open(cacheName).then((cache) => {
cache.addAll(assetsToCache);
});
});

self.addEventListener('push', function (event) {
sw.addEventListener('push', function (event) {
let payload = {};
try {
payload = event.data.json();
} catch (e) {
payload = { title: event.data.text() };
}
event.waitUntil(
self.registration.showNotification(payload.title, {
sw.registration.showNotification(payload.title, {
icon: '/logo-144.png',
body: payload.body,
data: payload.data,
}),
);
});

self.addEventListener('notificationclick', function (event) {
sw.addEventListener('notificationclick', function (event) {
if (event.notification.data?.url) {
self.clients.openWindow(event.notification.data.url);
sw.clients.openWindow(event.notification.data.url);
}
event.notification.close();
});

self.addEventListener('fetch', (event) => {
sw.addEventListener('fetch', (event) => {
const { request } = event;
const requestUrl = new URL(request.url);
const isSomeOrigin = requestUrl.origin === self.location.origin;
const isSomeOrigin = requestUrl.origin === sw.location.origin;
const isApiFetch = (isSomeOrigin && requestUrl.pathname.startsWith('/api')) || requestUrl.origin.includes('api.');

if (request.method !== 'GET') return;
if (!isSomeOrigin && !isApiFetch) return;

const getCache = () => self.caches.match(request, { ignoreSearch: request.mode === 'navigate' });
const getCache = () => sw.caches.match(request, { ignoreSearch: request.mode === 'navigate' });

event.respondWith(
(async function () {
Expand All @@ -55,7 +64,7 @@ self.addEventListener('fetch', (event) => {
.then(async (response) => {
if (response.ok) {
const responseCache = response.clone();
self.caches.open(cacheName).then((cache) => cache.put(request, responseCache));
sw.caches.open(cacheName).then((cache) => cache.put(request, responseCache));
return response;
} else {
const cache = await getCache();
Expand Down

0 comments on commit 90163ba

Please sign in to comment.