如何关闭Vitejs的热模块重新加载?
我有一个服务器端渲染生产
模式vite应用程序。我的问题是:通常将网页重新加载,并且控制台将显示[vite]连接...
。我将其追溯到Vite代码库的热模块重新加载部分。但是,我不希望hmr
用于production
,但是无论我将下面的两个设置设置为false
:
在我的vite.config.js
文件中,我有:
...
export default defineConfig({
server: {
hmr: false,
},
也:在我的nodejs server.js
文件中我有:
const vite = await createViteServer({
server: { middlewareMode: 'ssr', hmr: false },
})
如何关闭Vite的HMR
?
I have a server side rendered production
mode Vite application. My issue is that: routinely the webpage will reload and the console will display [vite] connecting...
. I traced this back to the hot module reload portion of vite's codebase. However, I do not want hmr
on for production
, but it still seems to be on regardless of me setting the two settings below to false
:
In my vite.config.js
file I have:
...
export default defineConfig({
server: {
hmr: false,
},
Also in my NodeJS server.js
file I have:
const vite = await createViteServer({
server: { middlewareMode: 'ssr', hmr: false },
})
How can I turn off Vite's hmr
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需修改
server
vite.config.ts中的属性:(这似乎是最简单的方法。)
信用: https://stackoverflow.com/a/77129725/152711
Just modify the
server
property in vite.config.ts:(This seems to be the simplest way.)
Credit: https://stackoverflow.com/a/77129725/152711
如果您希望完整重新加载而不是热模块重新加载,则可以将以下插件添加到
vite.config.ts
。以前的替代解决方案:
解决方案1
Vite具有 hmr api重新加载。您可能还对 hmr Server config 您的
vite.config.ts
文件。解决方案2
VITE还具有
- 禁用热模块重新加载的No-HMR
标志。解决方案3
不幸的是,解决方案1和2对我不起作用。这就是为什么我将以下代码添加到我的主/索引文件中,以在HMR发生后迫使浏览器重新加载。
You can add the following plugin to your
vite.config.ts
if you wish to full reload instead of hot module reload.Previous alternative solutions:
Solution 1
Vite has a HMR API that allows you to invalidate modules during a reload. You might also be interested in HMR Server Config which can be configured in your
vite.config.ts
file.Solution 2
Vite also had a
--no-hmr
flag for disabling hot module reloads.Solution 3
Unfortunately, both solution 1 and 2 didn't work for me. Which is why I added the following code to my main/index file for forcing the browser to reload after HMR took place.
将其添加到您的
app.js
文件:以及在您的
vite.config.js
文件中,文件添加 false'decteConfig':
Add this to your
app.js
file:and in your
vite.config.js
file addfalse
to'defineConfig'
: