fix: 取消服务器端 assets 智能 fallback 防止 MIME 错误,增强前端 chunk 失败后卸载 SW 并强制时间戳刷新,彻底解决旧缓存死锁白屏
This commit is contained in:
parent
590f37bb79
commit
952535cc0a
|
|
@ -94,19 +94,10 @@ const matchFile = async (request, response) => {
|
||||||
if (fs.existsSync(mappedPath)) {
|
if (fs.existsSync(mappedPath)) {
|
||||||
targetPath = mappedPath
|
targetPath = mappedPath
|
||||||
} else if (cleanPath.startsWith('/assets/')) {
|
} else if (cleanPath.startsWith('/assets/')) {
|
||||||
const filename = cleanPath.split('/').pop()
|
response.writeHead(404, { 'Content-Type': 'text/plain' })
|
||||||
const prefix = filename.split('-')[0]
|
response.end('Asset Not Found')
|
||||||
const ext = filename.split('.').pop()
|
resolve(true)
|
||||||
if (prefix && ext) {
|
return
|
||||||
const assetsDir = dist + '/assets'
|
|
||||||
if (fs.existsSync(assetsDir)) {
|
|
||||||
const files = fs.readdirSync(assetsDir)
|
|
||||||
const matchedFile = files.find(f => f.startsWith(prefix + '-') && f.endsWith('.' + ext))
|
|
||||||
if (matchedFile) {
|
|
||||||
targetPath = assetsDir + '/' + matchedFile
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fs.readFile(targetPath, (err, file) => {
|
fs.readFile(targetPath, (err, file) => {
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,23 @@ router.onError((error) => {
|
||||||
const isReloaded = sessionStorage.getItem('chunk_reload_flag')
|
const isReloaded = sessionStorage.getItem('chunk_reload_flag')
|
||||||
if (!isReloaded) {
|
if (!isReloaded) {
|
||||||
sessionStorage.setItem('chunk_reload_flag', 'true')
|
sessionStorage.setItem('chunk_reload_flag', 'true')
|
||||||
window.location.reload()
|
|
||||||
|
const forceReload = () => {
|
||||||
|
const url = new URL(window.location.href)
|
||||||
|
url.searchParams.set('t', Date.now())
|
||||||
|
window.location.href = url.href
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.getRegistrations().then(registrations => {
|
||||||
|
for (const registration of registrations) {
|
||||||
|
registration.unregister()
|
||||||
|
}
|
||||||
|
forceReload()
|
||||||
|
}).catch(() => { forceReload() })
|
||||||
|
} else {
|
||||||
|
forceReload()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue