Skip to content

Commit

Permalink
fixup! client: Add more type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Jul 30, 2023
1 parent 19faaeb commit 8a77c51
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions client/selfoss-sw-offline.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
/* eslint-env worker, serviceworker */
/// <reference lib="WebWorker" />

import { manifest, version } from '@parcel/service-worker';

async function install() {
// Default type of `self` is `WorkerGlobalScope & typeof globalThis`
// https://github.com/microsoft/TypeScript/issues/14877
declare const self: ServiceWorkerGlobalScope;

async function install(): Promise<undefined> {
const cache = await caches.open(version);

const entriesToCache =
const entriesToCache: string[] =
manifest
// We need to pass index.html through PHP to perform templating.
.map((entry) => entry === 'index.html' ? './' : entry);
.map((entry: string) => entry === 'index.html' ? './' : entry);

await cache.addAll(entriesToCache);
}
self.addEventListener('install', (event) => event.waitUntil(install()));
self.addEventListener('install', (event: ExtendableEvent) => event.waitUntil(install()));


async function activate() {
async function activate(): Promise<Array<boolean>> {
const keys = await caches.keys();
await Promise.all(
keys
.filter((key) => !(key === version || key === 'userCss' || key === 'userJs'))
.map((key) => caches.delete(key))
.filter((key: string) => !(key === version || key === 'userCss' || key === 'userJs'))
.map((key: string) => caches.delete(key))
);
}
self.addEventListener('activate', (event) => event.waitUntil(activate()));
self.addEventListener('activate', (event: ExtendableEvent) => event.waitUntil(activate()));


self.addEventListener('fetch', (event) => {
self.addEventListener('fetch', (event: FetchEvent) => {
if (event.request.method !== 'GET' || event.request.headers.get('X-Requested-With') === 'XMLHttpRequest') {
return;
}

event.respondWith(
caches.match(event.request)
.then((cachedResponse) => cachedResponse || fetch(event.request))
.then((cachedResponse: Response | undefined) => cachedResponse || fetch(event.request))
.catch(() => caches.match('./'))
);
});


self.addEventListener('message', (messageEvent) => {
self.addEventListener('message', (messageEvent: ExtendableMessageEvent) => {
if (messageEvent.data === 'skipWaiting') {
return self.skipWaiting();
}
Expand Down

0 comments on commit 8a77c51

Please sign in to comment.