From 052004d70ed6702fe7942c85ba766ff11e6ab453 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Fri, 19 Jul 2024 16:03:22 +0800 Subject: [PATCH] using compressImage when serviceWorker register error --- app/utils/chat.ts | 4 ++++ public/serviceWorkerRegister.js | 1 + 2 files changed, 5 insertions(+) diff --git a/app/utils/chat.ts b/app/utils/chat.ts index cde88043e10..77e9ee69be1 100644 --- a/app/utils/chat.ts +++ b/app/utils/chat.ts @@ -106,6 +106,10 @@ export function base64Image2Blob(base64Data: string, contentType: string) { } export function uploadImage(file: File): Promise { + if (!window._SW_ENABLED) { + // if serviceWorker register error, using compressImage + return compressImage(file, 256 * 1024); + } const body = new FormData(); body.append("file", file); return fetch(UPLOAD_URL, { diff --git a/public/serviceWorkerRegister.js b/public/serviceWorkerRegister.js index 4d0c5a69730..737205bb85f 100644 --- a/public/serviceWorkerRegister.js +++ b/public/serviceWorkerRegister.js @@ -15,6 +15,7 @@ if ('serviceWorker' in navigator) { registration.update().then(res => { console.log('ServiceWorker registration update: ', res); }); + window._SW_ENABLED = true }, function (err) { console.error('ServiceWorker registration failed: ', err); });