
failed to load module script:如何解决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.
如果访问根路径(如app.xxxx.cn)没有问题,而在非根路径(如https://app.xxx.cn/me/userinfo)刷新页面时出现此错误,可以从以下几个方面排查原因:
1. 检查base配置
在vite配置文件中,确保base配置正确,例如:
export default defineconfig({
base: './',
minify: true
});编译后的index.html中引入脚本的src应该是以./开头的。
2. 检查发布是否成功
确认是否已成功将项目发布到nginx服务器上。
3. 配置nginx rewrite
如果链接中不包含#,需要配置nginx的rewriteルール,例如:
location / {
try_files $uri $uri/ /index.html;
}4. 使用#号链接
如果链接使用#号的格式,结尾应该是/#/或/index.html#/。
以上就是Vite项目部署到Nginx非根路径刷新报错:如何解决Failed to load module script?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号