如何关闭Vitejs的热模块重新加载?

发布于 2025-02-05 19:31:02 字数 550 浏览 0 评论 0原文

我有一个服务器端渲染生产模式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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自在安然 2025-02-12 19:31:02

只需修改server vite.config.ts中的属性:(

server: {
    hmr: false
}

这似乎是最简单的方法。)

信用: https://stackoverflow.com/a/77129725/152711

Just modify the server property in vite.config.ts:

server: {
    hmr: false
}

(This seems to be the simplest way.)

Credit: https://stackoverflow.com/a/77129725/152711

三五鸿雁 2025-02-12 19:31:02

如果您希望完整重新加载而不是热模块重新加载,则可以将以下插件添加到vite.config.ts

{
    name: 'full-reload',
    handleHotUpdate({ server }) {
        server.ws.send({ type: "full-reload" })
        return [];
    }
}

以前的替代解决方案:

解决方案1

​​Vite具有 hmr api重新加载。您可能还对 hmr Server config 您的vite.config.ts文件。

// index.ts
if (import.meta.hot) {
    import.meta.hot.accept(() => {
        import.meta.hot.invalidate();
    })
}

解决方案2

VITE还具有- 禁用热模块重新加载的No-HMR标志。

vite --no-hmr

解决方案3

不幸的是,解决方案1和2对我不起作用。这就是为什么我将以下代码添加到我的主/索引文件中,以在HMR发生后迫使浏览器重新加载。

// index.ts
if (window['reload']) window.location = window.location;
window['reload'] = true;

You can add the following plugin to your vite.config.ts if you wish to full reload instead of hot module reload.

{
    name: 'full-reload',
    handleHotUpdate({ server }) {
        server.ws.send({ type: "full-reload" })
        return [];
    }
}

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.

// index.ts
if (import.meta.hot) {
    import.meta.hot.accept(() => {
        import.meta.hot.invalidate();
    })
}

Solution 2

Vite also had a --no-hmr flag for disabling hot module reloads.

vite --no-hmr

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.

// index.ts
if (window['reload']) window.location = window.location;
window['reload'] = true;
你げ笑在眉眼 2025-02-12 19:31:02

将其添加到您的app.js文件:

if (import.meta.hot)
import.meta.hot.accept(() => import.meta.hot.invalidate())

以及在您的vite.config.js文件中,文件添加 false 'decteConfig':

// server.hmr.overlay property
defineConfig({          
    server: {
        /*here*/
        hmr: { overlay: false }
    }, ...
  })

Add this to your app.js file:

if (import.meta.hot)
import.meta.hot.accept(() => import.meta.hot.invalidate())

and in your vite.config.js file add false to 'defineConfig':

// server.hmr.overlay property
defineConfig({          
    server: {
        /*here*/
        hmr: { overlay: false }
    }, ...
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文