Vite + Vue3 使用 npm run build
打包
完成打包使用Golang Gin框架进行代理,如下:
router.StaticFS("/static", http.Dir(filepath.Join(locExe, "web")))
问题描述
出现该问题
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of “text/plain”. Strict MIME type checking is enforced for module scripts per HTML spec.
这是由于Gin在返回.js
文件时,设置的Content-Type
为 text/plain; charset=utf-8
这导致了ES模块解析的时候因为类型不匹配导致。
解决
在Golang Gin的代理代码前配置MIME类型
_ = mime.AddExtensionType(".js", "application/javascript")
locExe, _ := filepath.Abs(filepath.Dir(os.Args[0]))
r.StaticFS("/static", http.Dir(filepath.Join(locExe, "web")))