From 2e6b86b226c8129f693e0326ca2644f20e0d43a8 Mon Sep 17 00:00:00 2001 From: SnowZ <13153320+zmtwiki@user.noreply.gitee.com> Date: Mon, 2 Feb 2026 16:28:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A0=B9=E6=8D=AE=20Content-Type=20?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E4=BF=AE=E6=AD=A3=E9=9D=9E=E6=A0=87=E5=87=86?= =?UTF-8?q?=20URL=20=E7=9A=84=E6=96=87=E4=BB=B6=E6=89=A9=E5=B1=95=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决 URL 不包含标准扩展名(如 hls.js@latest)时,浏览器可能无法正确识别 MIME 类型的问题。系统现在会根据响应的 Content-Type 自动追加 .css 或 .js 扩展名,确保文件被正确解析。 --- README.md | 7 +++++++ server.js | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 174180a..036fc43 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,13 @@ Github:https://github.com/cansnow123/Asset-cache - 本地保存:`cache/js/cdn.tailwindcss.com/index.js` - 对外访问:`/js/cdn.tailwindcss.com/index.js` +### 扩展名修正规则 + +- 针对 URL 不包含标准扩展名的情况(如 `hls.js@latest`),系统会根据响应的 `Content-Type` 强制修正: + - `text/css` → 强制追加 `.css` + - `application/javascript` 等 → 强制追加 `.js` +- 示例:`.../hls.js@latest` (js) → 本地保存为 `.../hls.js@latest.js`,确保浏览器能正确识别 MIME 类型。 + ### 字体与依赖资源处理(CSS 自动抓取) - 当抓取 `CSS` 文件时,会自动解析其中的 `url(...)` 引用,并尝试下载相对路径的依赖(如字体、图片等),统一保存到 `cache/css/...` 对应目录下,保持与源路径相同的层级结构。 diff --git a/server.js b/server.js index 4d32308..e3f297b 100644 --- a/server.js +++ b/server.js @@ -91,6 +91,19 @@ function resolveTargetPath(urlStr, contentType) { else if ((contentType || '').split(';')[0].trim() === 'text/css') type = 'css' else type = 'js' + // 增强修复:根据 Content-Type 强制修正扩展名(解决 hls.js@latest 等非标准后缀问题) + const ct = (contentType || '').split(';')[0].trim().toLowerCase() + const isCss = ct === 'text/css' + const isJs = ['application/javascript', 'application/x-javascript', 'text/javascript'].includes(ct) + + if (isCss && ext !== '.css') { + base += '.css' + ext = '.css' + } else if (isJs && ext !== '.js') { + base += '.js' + ext = '.js' + } + // 无扩展名时根据判定类型补全扩展名,确保同一URL在抓取前后路径一致 if (!ext) { if (type === 'css') { base = `${base}.css`; ext = '.css' }