fix: 根据 Content-Type 强制修正非标准 URL 的文件扩展名
解决 URL 不包含标准扩展名(如 hls.js@latest)时,浏览器可能无法正确识别 MIME 类型的问题。系统现在会根据响应的 Content-Type 自动追加 .css 或 .js 扩展名,确保文件被正确解析。
This commit is contained in:
@@ -144,6 +144,13 @@ Github:https://github.com/cansnow123/Asset-cache
|
|||||||
- 本地保存:`cache/js/cdn.tailwindcss.com/index.js`
|
- 本地保存:`cache/js/cdn.tailwindcss.com/index.js`
|
||||||
- 对外访问:`/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 自动抓取)
|
||||||
|
|
||||||
- 当抓取 `CSS` 文件时,会自动解析其中的 `url(...)` 引用,并尝试下载相对路径的依赖(如字体、图片等),统一保存到 `cache/css/...` 对应目录下,保持与源路径相同的层级结构。
|
- 当抓取 `CSS` 文件时,会自动解析其中的 `url(...)` 引用,并尝试下载相对路径的依赖(如字体、图片等),统一保存到 `cache/css/...` 对应目录下,保持与源路径相同的层级结构。
|
||||||
|
|||||||
13
server.js
13
server.js
@@ -91,6 +91,19 @@ function resolveTargetPath(urlStr, contentType) {
|
|||||||
else if ((contentType || '').split(';')[0].trim() === 'text/css') type = 'css'
|
else if ((contentType || '').split(';')[0].trim() === 'text/css') type = 'css'
|
||||||
else type = 'js'
|
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在抓取前后路径一致
|
// 无扩展名时根据判定类型补全扩展名,确保同一URL在抓取前后路径一致
|
||||||
if (!ext) {
|
if (!ext) {
|
||||||
if (type === 'css') { base = `${base}.css`; ext = '.css' }
|
if (type === 'css') { base = `${base}.css`; ext = '.css' }
|
||||||
|
|||||||
Reference in New Issue
Block a user