From 8c5ffa00ad7e637a89f476b0f1f71fa9c9dae5bb Mon Sep 17 00:00:00 2001 From: hzj <1304805162@qq.com> Date: Mon, 22 Dec 2025 17:33:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=9B=BE=E7=89=87=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=97=AE=E9=A2=98):=20=E4=BF=AE=E5=A4=8D=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E6=9C=89=E7=9A=84=E5=9B=BE=E7=89=87=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=AD=A3=E5=B8=B8=E7=BC=93=E5=AD=98=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/imageCacheManager.js | 52 ++++++++++++++++------------------ vite.config.js | 9 ++++++ 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/utils/imageCacheManager.js b/src/utils/imageCacheManager.js index 19834f5..4a11d98 100644 --- a/src/utils/imageCacheManager.js +++ b/src/utils/imageCacheManager.js @@ -17,7 +17,10 @@ class ImageCacheManager { // 根据资源类型选择缓存 async getCacheForUrl(url) { - if (url.startsWith('https://tkm-likei.oss-ap-southeast-1.aliyuncs.com/h5')) { + if ( + url.startsWith('https://tkm-likei.oss-ap-southeast-1.aliyuncs.com/h5') || + url.startsWith('/oss/h5/') + ) { return await caches.open(this.imageCacheName) } else { return await caches.open(this.assetCacheName) @@ -211,10 +214,8 @@ class ImageCacheManager { // 允许缓存特定域名下的资源 const ossBaseUrl = 'https://tkm-likei.oss-ap-southeast-1.aliyuncs.com/h5' - // 允许缓存开发环境下的代理路径 - const devProxyPath = '/oss/h5/' - if (url.startsWith(ossBaseUrl) || url.startsWith(devProxyPath)) { + if (url.startsWith(ossBaseUrl)) { return true } @@ -238,6 +239,12 @@ class ImageCacheManager { const response = await fetch(url, { mode: 'cors', credentials: 'omit', + cache: 'no-store', // 强制不使用 HTTP 缓存 + headers: { + 'Cache-Control': 'no-cache, no-store, must-revalidate', + Pragma: 'no-cache', + Expires: '0', + }, }) if (response.ok) { await cache.put(url, response.clone()) @@ -330,45 +337,36 @@ class ImageCacheManager { // 从网络加载图片 async loadImageFromNetwork(src, resolve, reject) { try { - // 对于OSS资源,先尝试直接创建Image对象(绕过fetch的CORS问题) - if (src.startsWith('https://tkm-likei.oss-ap-southeast-1.aliyuncs.com/h5')) { - const img = new Image() - img.crossOrigin = 'anonymous' - img.onload = () => { - resolve(img) - // 对于OSS资源,仍然尝试缓存(如果支持) - if (this.isBuildAsset(src)) { - this.cacheImageFromImage(src, img) - } - } - img.onerror = (error) => { - console.warn('图片加载失败:', src, error) - reject(error) - } - img.src = src - return - } - - // 对于其他资源,使用fetch + // 统一处理所有资源,不再对OSS做特殊处理 const response = await fetch(src, { mode: 'cors', credentials: 'omit', + cache: 'no-store', // 强制不使用 HTTP 缓存 + headers: { + 'Cache-Control': 'no-cache, no-store, must-revalidate', + Pragma: 'no-cache', + Expires: '0', + }, }) if (response.ok) { const img = new Image() img.onload = async () => { + console.log(`[ImageCacheManager] 图片加载成功: ${src}`) resolve(img) - // 缓存带有 ETag 的响应 + // 缓存响应 if (this.isBuildAsset(src)) { try { - await this.cacheImage(src) // 使用原始的缓存方法 + await this.cacheImage(src) // 使用标准缓存方法 } catch (cacheError) { console.warn('缓存图片时出错:', cacheError) } } } - img.onerror = reject + img.onerror = (error) => { + console.warn(`[ImageCacheManager] 图片 onload 失败: ${src}`, error) + reject(error) + } img.src = URL.createObjectURL(await response.blob()) } else { reject(new Error(`HTTP ${response.status}`)) diff --git a/vite.config.js b/vite.config.js index f3fa693..ca35134 100644 --- a/vite.config.js +++ b/vite.config.js @@ -51,6 +51,15 @@ export default defineConfig({ changeOrigin: true, secure: true, rewrite: (path) => path.replace(/^\/oss/, ''), + configure: (proxy, options) => { + proxy.on('proxyRes', (proxyRes, req, res) => { + // 移除缓存相关的响应头,防止浏览器使用HTTP缓存 + delete proxyRes.headers['etag'] + delete proxyRes.headers['last-modified'] + delete proxyRes.headers['cache-control'] + delete proxyRes.headers['expires'] + }) + }, }, // '/api': { // target: 'http://localhost:3000',