mirror of
https://github.com/paricafe/misskey.git
synced 2025-02-25 09:54:24 -06:00
fix sw
This commit is contained in:
parent
950b507a71
commit
0205565045
1 changed files with 200 additions and 203 deletions
|
@ -1,3 +1,8 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { get } from 'idb-keyval';
|
||||
import * as Misskey from 'misskey-js';
|
||||
import type { PushNotificationDataMap } from '@/types.js';
|
||||
|
@ -7,37 +12,38 @@ import { createEmptyNotification, createNotification } from '@/scripts/create-no
|
|||
import { swLang } from '@/scripts/lang.js';
|
||||
import * as swos from '@/scripts/operations.js';
|
||||
|
||||
const STATIC_ASSETS = [
|
||||
'/assets',
|
||||
'/emoji',
|
||||
'/twemoji',
|
||||
'/fluent-emoji',
|
||||
'/vite',
|
||||
];
|
||||
const CACHE_NAMES = {
|
||||
assets: `assets-${_VERSION_}`,
|
||||
emoji: `emoji-${_VERSION_}`,
|
||||
twemoji: `twemoji-${_VERSION_}`,
|
||||
fluentEmoji: `fluent-emoji-${_VERSION_}`,
|
||||
vite: `vite-${_VERSION_}`,
|
||||
locale: swLang.cacheName,
|
||||
};
|
||||
|
||||
const STATIC_CACHE_NAME = `static-cache-${_VERSION_}`;
|
||||
const LANG_CACHE_NAME = swLang.cacheName;
|
||||
const CACHE_PATHS = {
|
||||
[CACHE_NAMES.assets]: '/assets',
|
||||
[CACHE_NAMES.emoji]: '/emoji',
|
||||
[CACHE_NAMES.twemoji]: '/twemoji',
|
||||
[CACHE_NAMES.fluentEmoji]: '/fluent-emoji',
|
||||
[CACHE_NAMES.vite]: '/vite',
|
||||
};
|
||||
|
||||
globalThis.addEventListener('install', (ev) => {
|
||||
ev.waitUntil(
|
||||
(async () => {
|
||||
const cache = await caches.open(STATIC_CACHE_NAME);
|
||||
await cache.addAll(STATIC_ASSETS);
|
||||
})(),
|
||||
);
|
||||
// ev.waitUntil(globalThis.skipWaiting());
|
||||
});
|
||||
|
||||
globalThis.addEventListener('activate', ev => {
|
||||
ev.waitUntil(
|
||||
(async () => {
|
||||
const cacheNames = await caches.keys();
|
||||
const deletePromises = cacheNames
|
||||
.filter(name => name !== STATIC_CACHE_NAME && name !== LANG_CACHE_NAME)
|
||||
.map(name => caches.delete(name));
|
||||
|
||||
await Promise.all(deletePromises);
|
||||
await globalThis.clients.claim();
|
||||
})(),
|
||||
caches.keys()
|
||||
.then(cacheNames => Promise.all(
|
||||
cacheNames
|
||||
.filter((name) => {
|
||||
return !Object.values(CACHE_NAMES).includes(name);
|
||||
})
|
||||
.map(name => caches.delete(name)),
|
||||
))
|
||||
.then(() => globalThis.clients.claim()),
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -54,8 +60,28 @@ async function offlineContentHTML() {
|
|||
|
||||
globalThis.addEventListener('fetch', ev => {
|
||||
const url = new URL(ev.request.url);
|
||||
const matchedCache = Object.entries(CACHE_PATHS).find(([, path]) => url.pathname.startsWith(path));
|
||||
|
||||
const isStaticAsset = STATIC_ASSETS.some(path => url.pathname.startsWith(path));
|
||||
if (matchedCache) {
|
||||
ev.respondWith(
|
||||
caches.open(matchedCache[0])
|
||||
.then(cache =>
|
||||
cache.match(ev.request)
|
||||
.then(response => {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
return fetch(ev.request)
|
||||
.then(response => {
|
||||
cache.put(ev.request, response.clone());
|
||||
return response;
|
||||
});
|
||||
})
|
||||
)
|
||||
.catch(() => fetch(ev.request))
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let isHTMLRequest = false;
|
||||
if (ev.request.headers.get('sec-fetch-dest') === 'document') {
|
||||
|
@ -66,40 +92,7 @@ globalThis.addEventListener('fetch', ev => {
|
|||
isHTMLRequest = true;
|
||||
}
|
||||
|
||||
if (isStaticAsset) {
|
||||
ev.respondWith(
|
||||
caches.match(ev.request)
|
||||
.then(response => {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
return fetch(ev.request).then(response => {
|
||||
if (response.status === 200) {
|
||||
const responseClone = response.clone();
|
||||
caches.open(STATIC_CACHE_NAME).then(cache => {
|
||||
cache.put(ev.request, responseClone);
|
||||
});
|
||||
}
|
||||
return response;
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
if (isHTMLRequest) {
|
||||
return offlineContentHTML().then(html => {
|
||||
return new Response(html, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'text/html',
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isHTMLRequest) {
|
||||
if (!isHTMLRequest) return;
|
||||
ev.respondWith(
|
||||
fetch(ev.request)
|
||||
.catch(async () => {
|
||||
|
@ -112,11 +105,10 @@ globalThis.addEventListener('fetch', ev => {
|
|||
});
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
globalThis.addEventListener('push', ev => {
|
||||
// クライアント取得
|
||||
ev.waitUntil(globalThis.clients.matchAll({
|
||||
includeUncontrolled: true,
|
||||
type: 'window',
|
||||
|
@ -124,9 +116,12 @@ globalThis.addEventListener('push', ev => {
|
|||
const data: PushNotificationDataMap[keyof PushNotificationDataMap] = ev.data?.json();
|
||||
|
||||
switch (data.type) {
|
||||
// case 'driveFileCreated':
|
||||
case 'notification':
|
||||
case 'unreadAntennaNote':
|
||||
// 1日以上経過している場合は無視
|
||||
if (Date.now() - data.dateTime > 1000 * 60 * 60 * 24) break;
|
||||
|
||||
return createNotification(data);
|
||||
case 'readAllNotifications':
|
||||
await globalThis.registration.getNotifications()
|
||||
|
@ -246,14 +241,16 @@ globalThis.addEventListener('message', (ev: ServiceWorkerGlobalScopeEventMap['me
|
|||
ev.waitUntil((async (): Promise<void> => {
|
||||
switch (ev.data) {
|
||||
case 'clear':
|
||||
// Cache Storage全削除
|
||||
await caches.keys()
|
||||
.then(cacheNames => Promise.all(
|
||||
cacheNames.map(name => caches.delete(name)),
|
||||
));
|
||||
return;
|
||||
return; // TODO
|
||||
}
|
||||
|
||||
if (typeof ev.data === 'object') {
|
||||
// E.g. '[object Array]' → 'array'
|
||||
const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase();
|
||||
|
||||
if (otype === 'object') {
|
||||
|
|
Loading…
Add table
Reference in a new issue