Cache media only when storage can be freed (#7547)

This commit is contained in:
Akihiko Odaki 2018-05-20 02:22:11 +09:00 committed by Eugen Rochko
parent bcbb6aa46f
commit 4e1400cecb
2 changed files with 8 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import { freeStorage } from '../storage/modifier'; import { freeStorage, storageFreeable } from '../storage/modifier';
import './web_push_notifications'; import './web_push_notifications';
function openSystemCache() { function openSystemCache() {
@ -49,7 +49,7 @@ self.addEventListener('fetch', function(event) {
return response; return response;
})); }));
} else if (process.env.CDN_HOST ? url.host === process.env.CDN_HOST : url.pathname.startsWith('/system/')) { } else if (storageFreeable && process.env.CDN_HOST ? url.host === process.env.CDN_HOST : url.pathname.startsWith('/system/')) {
event.respondWith(openSystemCache().then(cache => { event.respondWith(openSystemCache().then(cache => {
return cache.match(event.request.url).then(cached => { return cache.match(event.request.url).then(cached => {
if (cached === undefined) { if (cached === undefined) {

View File

@ -4,6 +4,11 @@ const accountAssetKeys = ['avatar', 'avatar_static', 'header', 'header_static'];
const storageMargin = 8388608; const storageMargin = 8388608;
const storeLimit = 1024; const storeLimit = 1024;
// navigator.storage is not present on:
// Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.100 Safari/537.36 Edge/16.16299
// estimate method is not present on Chrome 57.0.2987.98 on Linux.
export const storageFreeable = 'storage' in navigator && 'estimate' in navigator.storage;
function openCache() { function openCache() {
// ServiceWorker and Cache API is not available on iOS 11 // ServiceWorker and Cache API is not available on iOS 11
// https://webkit.org/status/#specification-service-workers // https://webkit.org/status/#specification-service-workers
@ -182,10 +187,7 @@ export function putStatuses(records) {
} }
export function freeStorage() { export function freeStorage() {
// navigator.storage is not present on: return storageFreeable && navigator.storage.estimate().then(({ quota, usage }) => {
// Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.100 Safari/537.36 Edge/16.16299
// estimate method is not present on Chrome 57.0.2987.98 on Linux.
return 'storage' in navigator && 'estimate' in navigator.storage && navigator.storage.estimate().then(({ quota, usage }) => {
if (usage + storageMargin < quota) { if (usage + storageMargin < quota) {
return null; return null;
} }