fix: 修复静态图片资源 MIME 解析及代理匹配优先级引发的显示异常
This commit is contained in:
parent
8bab17d01e
commit
aafa8e2509
|
|
@ -55,14 +55,42 @@ const matchRouter = async (request, response) => {
|
|||
return false
|
||||
}
|
||||
}
|
||||
function getMimeType(filePath) {
|
||||
const ext = filePath.split('.').pop()?.toLowerCase()
|
||||
const defaultMimes = {
|
||||
png: 'image/png',
|
||||
jpg: 'image/jpeg',
|
||||
jpeg: 'image/jpeg',
|
||||
gif: 'image/gif',
|
||||
webp: 'image/webp',
|
||||
svg: 'image/svg+xml',
|
||||
ico: 'image/x-icon',
|
||||
js: 'text/javascript',
|
||||
css: 'text/css',
|
||||
html: 'text/html',
|
||||
json: 'application/json',
|
||||
woff: 'font/woff',
|
||||
woff2: 'font/woff2',
|
||||
ttf: 'font/ttf',
|
||||
otf: 'font/otf'
|
||||
}
|
||||
if (ext && defaultMimes[ext]) {
|
||||
return defaultMimes[ext]
|
||||
}
|
||||
if (mime && typeof mime.getType === 'function') {
|
||||
return mime.getType(filePath) || 'application/octet-stream'
|
||||
}
|
||||
return 'application/octet-stream'
|
||||
}
|
||||
|
||||
const matchFile = async (request, response) => {
|
||||
return new Promise((resolve) => {
|
||||
const path = request.url.split('?')[0]
|
||||
fs.readFile(dist + decodeURIComponent(path), (err, file) => {
|
||||
const cleanPath = decodeURIComponent(request.url.split('?')[0])
|
||||
fs.readFile(dist + cleanPath, (err, file) => {
|
||||
if (!err) {
|
||||
const mimetype = mime.getType(request.url)
|
||||
const mimetype = getMimeType(cleanPath)
|
||||
const responseHdeaders = { 'Content-Type': mimetype }
|
||||
if (path.endsWith("-sw.js")) {
|
||||
if (cleanPath.endsWith("-sw.js")) {
|
||||
responseHdeaders["Service-Worker-Allowed"] = '/'
|
||||
}
|
||||
response.writeHead(httpOk, responseHdeaders)
|
||||
|
|
@ -92,14 +120,14 @@ const matchType = async (request, response) => {
|
|||
if (isRouter) {
|
||||
return
|
||||
}
|
||||
let isProxy = matchProxy(request, response)
|
||||
if (isProxy) {
|
||||
return
|
||||
}
|
||||
let isFile = await matchFile(request, response)
|
||||
if (isFile) {
|
||||
return
|
||||
}
|
||||
let isProxy = matchProxy(request, response)
|
||||
if (isProxy) {
|
||||
return
|
||||
}
|
||||
await endIndex(request, response)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue