fix: 资源加载失败时自动刷新页面
- 添加路由错误处理,当动态导入模块失败时自动刷新页面 - 修复 assets 文件不存在时返回 404 错误
This commit is contained in:
@@ -262,6 +262,13 @@ func setupEmbeddedFrontend(r *gin.Engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.GET("/assets/*filepath", func(c *gin.Context) {
|
r.GET("/assets/*filepath", func(c *gin.Context) {
|
||||||
|
filepath := strings.TrimPrefix(c.Request.URL.Path, "/assets/")
|
||||||
|
file, err := distFS.Open(filepath)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(404, gin.H{"error": "asset not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
c.Header("Cache-Control", "public, max-age=31536000, immutable")
|
c.Header("Cache-Control", "public, max-age=31536000, immutable")
|
||||||
c.FileFromFS(c.Request.URL.Path, http.FS(distFS))
|
c.FileFromFS(c.Request.URL.Path, http.FS(distFS))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ const router = createRouter({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.onError((error) => {
|
||||||
|
if (error.message.includes('Failed to fetch dynamically imported module')) {
|
||||||
|
window.location.reload()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
createRouterGuard(router)
|
createRouterGuard(router)
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|||||||
Reference in New Issue
Block a user