mirror of
https://github.com/paricafe/misskey.git
synced 2025-02-17 13:37:29 -06:00
sw: increase size cache and auto clean
This commit is contained in:
parent
e8d04d1029
commit
e4461c444c
2 changed files with 45 additions and 11 deletions
|
@ -215,14 +215,14 @@ export class SearchService {
|
|||
query.andWhere('note.channelId = :channelId', { channelId: opts.channelId });
|
||||
}
|
||||
|
||||
makeQuery(query);
|
||||
|
||||
query
|
||||
.andWhere('note.text &@~ :q', { q: sqlLikeEscape(q) })
|
||||
.innerJoinAndSelect('note.user', 'user')
|
||||
.leftJoinAndSelect('note.reply', 'reply')
|
||||
.leftJoinAndSelect('note.renote', 'renote')
|
||||
.leftJoinAndSelect('reply.user', 'replyUser')
|
||||
.leftJoinAndSelect('renote.user', 'renoteUser');
|
||||
makeQuery(query);
|
||||
|
||||
if (opts.host) {
|
||||
if (opts.host === '.') {
|
||||
|
|
|
@ -15,6 +15,22 @@ import * as swos from '@/scripts/operations.js';
|
|||
const STATIC_CACHE_NAME = `misskey-static-${_VERSION_}`;
|
||||
const PATHS_TO_CACHE = ['/assets/', '/static-assets/', '/emoji/', '/twemoji/', '/fluent-emoji/', '/vite/'];
|
||||
|
||||
async function requestStorage() {
|
||||
try {
|
||||
if (navigator.storage && navigator.storage.persist) {
|
||||
await navigator.storage.persist();
|
||||
}
|
||||
if (navigator.storage && navigator.storage.estimate) {
|
||||
const quota = await navigator.storage.estimate();
|
||||
if (quota.quota && quota.quota < 2 * 1024 * 1024 * 1024) {
|
||||
await navigator.storage.requestQuota(2 * 1024 * 1024 * 1024);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
console.error('Failed to request storage');
|
||||
}
|
||||
}
|
||||
|
||||
async function cacheWithFallback(cache, paths) {
|
||||
for (const path of paths) {
|
||||
try {
|
||||
|
@ -25,6 +41,7 @@ async function cacheWithFallback(cache, paths) {
|
|||
|
||||
globalThis.addEventListener('install', (ev) => {
|
||||
ev.waitUntil((async () => {
|
||||
await requestStorage();
|
||||
const cache = await caches.open(STATIC_CACHE_NAME);
|
||||
await cacheWithFallback(cache, PATHS_TO_CACHE);
|
||||
await globalThis.skipWaiting();
|
||||
|
@ -60,18 +77,35 @@ globalThis.addEventListener('fetch', ev => {
|
|||
if (shouldCache) {
|
||||
ev.respondWith(
|
||||
caches.match(ev.request)
|
||||
.then(response => {
|
||||
.then(async response => {
|
||||
if (response) return response;
|
||||
|
||||
return fetch(ev.request).then(response => {
|
||||
if (!response || response.status !== 200 || response.type !== 'basic') return response;
|
||||
const responseToCache = response.clone();
|
||||
caches.open(STATIC_CACHE_NAME)
|
||||
.then(cache => {
|
||||
cache.put(ev.request, responseToCache);
|
||||
});
|
||||
try {
|
||||
const fetchResponse = await fetch(ev.request);
|
||||
if (!fetchResponse || fetchResponse.status !== 200 || fetchResponse.type !== 'basic') {
|
||||
return fetchResponse;
|
||||
}
|
||||
|
||||
const responseToCache = fetchResponse.clone();
|
||||
const cache = await caches.open(STATIC_CACHE_NAME);
|
||||
|
||||
try {
|
||||
await cache.put(ev.request, responseToCache);
|
||||
} catch (err) {
|
||||
const keys = await cache.keys();
|
||||
if (keys.length > 0) {
|
||||
const deleteCount = Math.ceil(keys.length * 0.2);
|
||||
for (let i = 0; i < deleteCount; i++) {
|
||||
await cache.delete(keys[i]);
|
||||
}
|
||||
await cache.put(ev.request, responseToCache.clone());
|
||||
}
|
||||
}
|
||||
|
||||
return fetchResponse;
|
||||
} catch {
|
||||
return response;
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
return;
|
||||
|
|
Loading…
Add table
Reference in a new issue