Vite项目部署到Nginx后刷新页面报错:模块脚本加载失败的解决方案
将使用Vite构建的项目部署到Nginx服务器后,访问非根路径并刷新页面,常常会遇到“failed to load module script: expected a javascript module script but the server responded with a mime type of “text/html”. strict mime type checking is enforced for module scripts per html spec.”错误。本文将分析此问题并提供解决方法。
问题描述:
在将Vite项目部署到app.xxxx.cn域名后,访问根路径app.xxxx.cn正常。但访问非根路径(例如app.xxxx.cn/me/userinfo)并刷新页面时,出现上述错误。错误信息表明浏览器期望JavaScript模块脚本,但服务器返回了text/html响应。这通常是服务器未正确处理静态资源请求或前端路由配置问题。
示例vite.config.js配置:
export default defineConfig({ base: './', minify: true})
登录后复制
本文来自互联网或AI生成,不代表软件指南立场。本站不负任何法律责任。