From f1f97082dcdbb080b9fd715d318ff945dbf73792 Mon Sep 17 00:00:00 2001 From: Pysio Date: Wed, 1 Jan 2025 19:32:20 +0800 Subject: [PATCH] feat: add cache --- src/index.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index d8b8276..f28e52f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -492,7 +492,7 @@ app.get('/images/:hash', async (req: Request, res: Response): Promise => { } }); -// 添加通过hash直接展示图片的接口 +// 修改通过hash直接展示图片的接口 app.get('/i/:hash', async (req: Request, res: Response): Promise => { try { const { hash } = req.params; @@ -503,17 +503,22 @@ app.get('/i/:hash', async (req: Request, res: Response): Promise => { return; } + // 设置强缓存和CDN相关的响应头 res.set({ 'Content-Type': 'image/webp', 'Content-Disposition': `inline; filename="${hash}.webp"`, - 'Cache-Control': 'public, max-age=31536000', // 缓存一年 - 'ETag': `"${hash}"` // 添加ETag支持 + 'Cache-Control': 'public, max-age=31536000, immutable', // 一年缓存,添加immutable + 'ETag': `"${hash}"`, + 'CDN-Cache-Control': 'max-age=31536000', // 专门针对CDN的缓存控制 + 'Surrogate-Control': 'max-age=31536000', // 用于CDN缓存控制 + 'Access-Control-Allow-Origin': '*', // 允许跨域访问 + 'Vary': 'Accept' // 基于Accept头的内容协商 }); // 检查浏览器缓存 const ifNoneMatch = req.header('If-None-Match'); if (ifNoneMatch === `"${hash}"`) { - res.status(304).send(); // Not Modified + res.status(304).send(); return; }