从 SSR 场景看 HMR 的另一类失效
如果项目用了 Vite 的 SSR 模式(比如 Nuxt、Vite SSR 脚手架或自己搭的 `ssrLoadModule` 流程),HMR 的行为会和纯客户端项目有明显差别。SSR 下模块会在服务端被 `ssrLoadModule` 加载一次,客户端再加载一次,两套模块图是分开的。改了一个只在服务端使用的文件,比如 API 路由或数据获取函数,浏览器端可能完全收不到更新,因为客户端模块图里根本没有这个模块。 ```js // server/api/posts.js —— 只在 SSR 时被 ssrLoadModule 加载 export async function getPosts() { return fetch('https://api.example.com/posts').then(r => r.json()) } ``` 改动这个文件后,Vite 会触发服务端模块失效,但客户端不一定会刷新。如果希望改动后页面重新拉数据,需要在客户端对应的组件里显式监听: ```js if (import.meta.hot) { import.meta.hot.accept(['./server/api/posts.js'], () => { // 重新触发数据加载 reloadPosts() }) } ``` 这种“跨运行时”的 HMR 失效很难通过前面的客户端链路排查出来,因为终端确实打印了服务端模块更新,但浏览器端就是不动。判断依据是看文件是不是只被 SSR 侧引用。 ## CSS 更新看起来生效了,其实没有 CSS 的 HMR 走的是独立通道,Vite 会直接把新的样式通过 WebSocket 推给客户端,替换 `` 标签内容,不会重新执行 JS。这套机制在大多数情况下很快,但有两种情况会让人误以为 HMR 坏了。 一种是 `` 里用了 `:deep()` 或动态 class,样式替换后由于组件没有重新渲染,新的选择器没有匹配到更新后的 DOM。另一种是样式被 `@import` 引入,而 Vite 对 `@import` 的处理是内联展开,改动被 import 的文件时,如果内联结果被缓存,就不会触发更新。 ```css /* main.css */ @import './theme.css'; /* theme.css 改动后,有时 main.css 的 HMR 不会触发 */ ``` 遇到这种情况,把 `@import` 换成在 JS 里 import CSS,或者确认 `css.devSourcemap` 配置没有干扰内联逻辑,通常能恢复。 ## 手动触发一次全量模块失效 排查到后面,如果实在定位不到具体哪个模块卡住了,可以用一个临时手段确认问题范围:在浏览器控制台里拿到 Vite 的 HMR 客户端实例,手动让它失效某个模块。 ```js // 浏览器控制台 const mod = import.meta.hot // 或者在页面上挂一个全局引用方便调试 window.__vite_hmr__ = import.meta.hot ``` 更直接的做法是在入口文件里临时加一段: ```js if (import.meta.hot) { import.meta.hot.on('vite:beforeUpdate', (payload) => { console.log('HMR payload', payload) }) } ``` `vite:beforeUpdate` 会在每次更新前触发,payload 里包含 `updates` 数组,能看到 Vite 认为哪些模块需要更新。如果这个事件压根不触发,说明问题在服务端推送之前;如果触发了但 `updates` 为空,说明 Vite 没找到需要更新的模块,大概率是模块图里没有这个文件,或者文件被某种方式排除了。 ## 生产构建正常不代表 HMR 正常 一个容易踩的坑是:用 `vite build` 验证没问题,就以为 HMR 也是好的。构建走的是 Rollup,HMR 走的是 Vite 自己的 dev server 和模块图,两者对模块的处理方式不同。构建时会被 tree-shaking 掉的代码,在 dev 下仍然存在并参与 HMR 传播;反过来,dev 下被 `optimizeDeps` 预构建的依赖,构建时会被正常打包。所以构建通过只能说明代码语法和类型没问题,不能作为 HMR 正常的依据。 真正要验证 HMR,得在 dev server 跑起来的情况下,实际改几个不同类型的文件:一个 Vue/React 组件、一个工具函数、一个 CSS 文件、一个被预构建的依赖,看每种情况下浏览器和控制台的反应。这比单看构建结果可靠得多。 ## 缓存清理的边界 前面提到清 `node_modules/.vite` 能解决预构建缓存问题,但不要把它当成万能药。这个目录只缓存依赖预构建结果,不缓存源码模块。源码的 HMR 状态在内存里,重启 dev server 就会重置。如果重启后问题还在,清 `.vite` 基本没用,应该往模块图和插件方向查。 另外,浏览器端也有缓存。Vite dev server 对源码模块默认发送 `Cache-Control: no-cache`,但某些代理或 Service Worker 可能拦截请求。如果项目里注册了 Service Worker,先确认它没有缓存 dev 环境的资源: ```js // 只在生产环境注册 if ('serviceWorker' in navigator && import.meta.env.PROD) { navigator.serviceWorker.register('/sw.js') } ``` 排查 HMR 时如果怀疑是浏览器缓存,开一个无痕窗口是最快的验证方式。无痕窗口不加载已有 Service Worker 和扩展,能直接排除这两类干扰。 ## 把排查经验固化下来 HMR 失效的排查成本主要来自信息不足,而不是问题本身复杂。与其每次靠记忆重走一遍链路,不如在项目里加一个小的调试开关,在需要时打开 HMR 日志。 ```js // vite.config.js export default { define: { __HMR_DEBUG__: JSON.stringify(process.env.HMR_DEBUG === 'true'), }, } ``` ```js // 入口文件 if (__HMR_DEBUG__ && import.meta.hot) { import.meta.hot.on('vite:beforeUpdate', (p) => console.log('[hmr] update', p)) import.meta.hot.on('vite:error', (e) => console.error('[hmr] error', e)) import.meta.hot.on('vite:ws:disconnect', () => console.warn('[hmr] ws disconnected')) } ``` 平时这个开关关着,不产生噪音;一旦出现“改了没反应”,打开它就能看到 Vite 到底推了什么、WebSocket 有没有断。把这条日志和终端里的 `hmr update` 对照,能省掉大量猜测时间。 推荐查看天气:http://www.362tq.com/wujin/10tian.html 推荐查看天气:http://www.362tq.com/wujin/15tian.html 推荐查看天气:http://www.362tq.com/wujin/40tian.html 推荐查看天气:http://www.362tq.com/liyang/ 推荐查看天气:http://www.362tq.com/liyang/zuotian.html 推荐查看天气:http://www.362tq.com/liyang/7tian.html 推荐查看天气:http://www.362tq.com/liyang/10tian.html 推荐查看天气:http://www.362tq.com/liyang/15tian.html 推荐查看天气:http://www.362tq.com/liyang/40tian.html 推荐查看天气:http://www.362tq.com/jintan/ 推荐查看天气:http://www.362tq.com/jintan/zuotian.html 推荐查看天气:http://www.362tq.com/jintan/7tian.html 推荐查看天气:http://www.362tq.com/jintan/10tian.html 推荐查看天气:http://www.362tq.com/jintan/15tian.html 推荐查看天气:http://www.362tq.com/jintan/40tian.html 推荐查看天气:http://www.362tq.com/suzhou/ 推荐查看天气:http://www.362tq.com/suzhou/zuotian.html 推荐查看天气:http://www.362tq.com/suzhou/7tian.html 推荐查看天气:http://www.362tq.com/suzhou/10tian.html 推荐查看天气:http://www.362tq.com/suzhou/15tian.html 推荐查看天气:http://www.362tq.com/suzhou/40tian.html 推荐查看天气:http://www.362tq.com/huqiu/ 推荐查看天气:http://www.362tq.com/huqiu/zuotian.html 推荐查看天气:http://www.362tq.com/huqiu/7tian.html 推荐查看天气:http://www.362tq.com/huqiu/10tian.html 推荐查看天气:http://www.362tq.com/huqiu/15tian.html 推荐查看天气:http://www.362tq.com/huqiu/40tian.html 推荐查看天气:http://www.362tq.com/suzhouwuzhong/ 推荐查看天气:http://www.362tq.com/suzhouwuzhong/zuotian.html 推荐查看天气:http://www.362tq.com/suzhouwuzhong/7tian.html 推荐查看天气:http://www.362tq.com/suzhouwuzhong/10tian.html 推荐查看天气:http://www.362tq.com/suzhouwuzhong/15tian.html 推荐查看天气:http://www.362tq.com/suzhouwuzhong/40tian.html 推荐查看天气:http://www.362tq.com/changshu/ 推荐查看天气:http://www.362tq.com/changshu/zuotian.html 推荐查看天气:http://www.362tq.com/changshu/7tian.html 推荐查看天气:http://www.362tq.com/changshu/10tian.html 推荐查看天气:http://www.362tq.com/changshu/15tian.html 推荐查看天气:http://www.362tq.com/changshu/40tian.html 推荐查看天气:http://www.362tq.com/zhangjiagang/ 推荐查看天气:http://www.362tq.com/zhangjiagang/zuotian.html 推荐查看天气:http://www.362tq.com/zhangjiagang/7tian.html 推荐查看天气:http://www.362tq.com/zhangjiagang/10tian.html 推荐查看天气:http://www.362tq.com/zhangjiagang/15tian.html 推荐查看天气:http://www.362tq.com/zhangjiagang/40tian.html 推荐查看天气:http://www.362tq.com/kunshan/ 推荐查看天气:http://www.362tq.com/kunshan/zuotian.html 推荐查看天气:http://www.362tq.com/kunshan/7tian.html 推荐查看天气:http://www.362tq.com/kunshan/10tian.html 推荐查看天气:http://www.362tq.com/kunshan/15tian.html 推荐查看天气:http://www.362tq.com/kunshan/40tian.html 推荐查看天气:http://www.362tq.com/suzhouwujiang/ 推荐查看天气:http://www.362tq.com/suzhouwujiang/zuotian.html 推荐查看天气:http://www.362tq.com/suzhouwujiang/7tian.html 推荐查看天气:http://www.362tq.com/suzhouwujiang/10tian.html 推荐查看天气:http://www.362tq.com/suzhouwujiang/15tian.html 推荐查看天气:http://www.362tq.com/suzhouwujiang/40tian.html 推荐查看天气:http://www.362tq.com/taicang/ 推荐查看天气:http://www.362tq.com/taicang/zuotian.html 推荐查看天气:http://www.362tq.com/taicang/7tian.html 推荐查看天气:http://www.362tq.com/taicang/10tian.html 推荐查看天气:http://www.362tq.com/taicang/15tian.html 推荐查看天气:http://www.362tq.com/taicang/40tian.html 推荐查看天气:http://www.362tq.com/nantong/ 推荐查看天气:http://www.362tq.com/nantong/zuotian.html 推荐查看天气:http://www.362tq.com/nantong/7tian.html 推荐查看天气:http://www.362tq.com/nantong/10tian.html 推荐查看天气:http://www.362tq.com/nantong/15tian.html 推荐查看天气:http://www.362tq.com/nantong/40tian.html 推荐查看天气:http://www.362tq.com/chongchuan/ 推荐查看天气:http://www.362tq.com/chongchuan/zuotian.html 推荐查看天气:http://www.362tq.com/chongchuan/7tian.html 推荐查看天气:http://www.362tq.com/chongchuan/10tian.html 推荐查看天气:http://www.362tq.com/chongchuan/15tian.html 推荐查看天气:http://www.362tq.com/chongchuan/40tian.html 推荐查看天气:http://www.362tq.com/gangzha/ 推荐查看天气:http://www.362tq.com/gangzha/zuotian.html 推荐查看天气:http://www.362tq.com/gangzha/7tian.html 推荐查看天气:http://www.362tq.com/gangzha/10tian.html 推荐查看天气:http://www.362tq.com/gangzha/15tian.html 推荐查看天气:http://www.362tq.com/gangzha/40tian.html 推荐查看天气:http://www.362tq.com/haian/ 推荐查看天气:http://www.362tq.com/haian/zuotian.html 推荐查看天气:http://www.362tq.com/haian/7tian.html 推荐查看天气:http://www.362tq.com/haian/10tian.html 推荐查看天气:http://www.362tq.com/haian/15tian.html 推荐查看天气:http://www.362tq.com/haian/40tian.html 推荐查看天气:http://www.362tq.com/rudong/ 推荐查看天气:http://www.362tq.com/rudong/zuotian.html 推荐查看天气:http://www.362tq.com/rudong/7tian.html 推荐查看天气:http://www.362tq.com/rudong/10tian.html 推荐查看天气:http://www.362tq.com/rudong/15tian.html 推荐查看天气:http://www.362tq.com/rudong/40tian.html 推荐查看天气:http://www.362tq.com/nantongqidong/ 推荐查看天气:http://www.362tq.com/nantongqidong/zuotian.html 推荐查看天气:http://www.362tq.com/nantongqidong/7tian.html 推荐查看天气:http://www.362tq.com/nantongqidong/10tian.html 推荐查看天气:http://www.362tq.com/nantongqidong/15tian.html 推荐查看天气:http://www.362tq.com/nantongqidong/40tian.html 推荐查看天气:http://www.362tq.com/rugao/ 推荐查看天气:http://www.362tq.com/rugao/zuotian.html 推荐查看天气:http://www.362tq.com/rugao/7tian.html 推荐查看天气:http://www.362tq.com/rugao/10tian.html 推荐查看天气:http://www.362tq.com/rugao/15tian.html 推荐查看天气:http://www.362tq.com/rugao/40tian.html 推荐查看天气:http://www.362tq.com/tongzhou/ 推荐查看天气:http://www.362tq.com/tongzhou/zuotian.html 推荐查看天气:http://www.362tq.com/tongzhou/7tian.html 推荐查看天气:http://www.362tq.com/tongzhou/10tian.html 推荐查看天气:http://www.362tq.com/tongzhou/15tian.html 推荐查看天气:http://www.362tq.com/tongzhou/40tian.html 推荐查看天气:http://www.362tq.com/haimen/ 推荐查看天气:http://www.362tq.com/haimen/zuotian.html 推荐查看天气:http://www.362tq.com/haimen/7tian.html 推荐查看天气:http://www.362tq.com/haimen/10tian.html 推荐查看天气:http://www.362tq.com/haimen/15tian.html 推荐查看天气:http://www.362tq.com/haimen/40tian.html 推荐查看天气:http://www.362tq.com/lianyungang/ 推荐查看天气:http://www.362tq.com/lianyungang/zuotian.html 推荐查看天气:http://www.362tq.com/lianyungang/7tian.html 推荐查看天气:http://www.362tq.com/lianyungang/10tian.html 推荐查看天气:http://www.362tq.com/lianyungang/15tian.html 推荐查看天气:http://www.362tq.com/lianyungang/40tian.html 推荐查看天气:http://www.362tq.com/haizhou/ 推荐查看天气:http://www.362tq.com/haizhou/zuotian.html 推荐查看天气:http://www.362tq.com/haizhou/7tian.html 推荐查看天气:http://www.362tq.com/haizhou/10tian.html 推荐查看天气:http://www.362tq.com/haizhou/15tian.html 推荐查看天气:http://www.362tq.com/haizhou/40tian.html 推荐查看天气:http://www.362tq.com/ganyu/ 推荐查看天气:http://www.362tq.com/ganyu/zuotian.html 推荐查看天气:http://www.362tq.com/ganyu/7tian.html 推荐查看天气:http://www.362tq.com/ganyu/10tian.html 推荐查看天气:http://www.362tq.com/ganyu/15tian.html 推荐查看天气:http://www.362tq.com/ganyu/40tian.html 推荐查看天气:http://www.362tq.com/donghai/ 推荐查看天气:http://www.362tq.com/donghai/zuotian.html 推荐查看天气:http://www.362tq.com/donghai/7tian.html 推荐查看天气:http://www.362tq.com/donghai/10tian.html 推荐查看天气:http://www.362tq.com/donghai/15tian.html 推荐查看天气:http://www.362tq.com/donghai/40tian.html 推荐查看天气:http://www.362tq.com/guanyun/ 推荐查看天气:http://www.362tq.com/guanyun/zuotian.html 推荐查看天气:http://www.362tq.com/guanyun/7tian.html 推荐查看天气:http://www.362tq.com/guanyun/10tian.html 推荐查看天气:http://www.362tq.com/guanyun/15tian.html 推荐查看天气:http://www.362tq.com/guanyun/40tian.html 推荐查看天气:http://www.362tq.com/guannan/ 推荐查看天气:http://www.362tq.com/guannan/zuotian.html 推荐查看天气:http://www.362tq.com/guannan/7tian.html 推荐查看天气:http://www.362tq.com/guannan/10tian.html 推荐查看天气:http://www.362tq.com/guannan/15tian.html 推荐查看天气:http://www.362tq.com/guannan/40tian.html 推荐查看天气:http://www.362tq.com/huaian/ 推荐查看天气:http://www.362tq.com/huaian/zuotian.html 推荐查看天气:http://www.362tq.com/huaian/7tian.html 推荐查看天气:http://www.362tq.com/huaian/10tian.html 推荐查看天气:http://www.362tq.com/huaian/15tian.html 推荐查看天气:http://www.362tq.com/huaian/40tian.html 推荐查看天气:http://www.362tq.com/chuzhouqu/ 推荐查看天气:http://www.362tq.com/chuzhouqu/zuotian.html 推荐查看天气:http://www.362tq.com/chuzhouqu/7tian.html 推荐查看天气:http://www.362tq.com/chuzhouqu/10tian.html 推荐查看天气:http://www.362tq.com/chuzhouqu/15tian.html 推荐查看天气:http://www.362tq.com/chuzhouqu/40tian.html 推荐查看天气:http://www.362tq.com/huaianhuaiyin/ 推荐查看天气:http://www.362tq.com/huaianhuaiyin/zuotian.html 推荐查看天气:http://www.362tq.com/huaianhuaiyin/7tian.html 推荐查看天气:http://www.362tq.com/huaianhuaiyin/10tian.html 推荐查看天气:http://www.362tq.com/huaianhuaiyin/15tian.html 推荐查看天气:http://www.362tq.com/huaianhuaiyin/40tian.html 推荐查看天气:http://www.362tq.com/qingpu/ 推荐查看天气:http://www.362tq.com/qingpu/zuotian.html 推荐查看天气:http://www.362tq.com/qingpu/7tian.html 推荐查看天气:http://www.362tq.com/qingpu/10tian.html 推荐查看天气:http://www.362tq.com/qingpu/15tian.html 推荐查看天气:http://www.362tq.com/qingpu/40tian.html 推荐查看天气:http://www.362tq.com/lianshui/ 推荐查看天气:http://www.362tq.com/lianshui/zuotian.html 推荐查看天气:http://www.362tq.com/lianshui/7tian.html 推荐查看天气:http://www.362tq.com/lianshui/10tian.html 推荐查看天气:http://www.362tq.com/lianshui/15tian.html 推荐查看天气:http://www.362tq.com/lianshui/40tian.html 推荐查看天气:http://www.362tq.com/hongze/ 推荐查看天气:http://www.362tq.com/hongze/zuotian.html 推荐查看天气:http://www.362tq.com/hongze/7tian.html 推荐查看天气:http://www.362tq.com/hongze/10tian.html 推荐查看天气:http://www.362tq.com/hongze/15tian.html 推荐查看天气:http://www.362tq.com/hongze/40tian.html 推荐查看天气:http://www.362tq.com/xuchi/ 推荐查看天气:http://www.362tq.com/xuchi/zuotian.html 推荐查看天气:http://www.362tq.com/xuchi/7tian.html 推荐查看天气:http://www.362tq.com/xuchi/10tian.html 推荐查看天气:http://www.362tq.com/xuchi/15tian.html 推荐查看天气:http://www.362tq.com/xuchi/40tian.html 推荐查看天气:http://www.362tq.com/jinhu/ 推荐查看天气:http://www.362tq.com/jinhu/zuotian.html 推荐查看天气:http://www.362tq.com/jinhu/7tian.html 推荐查看天气:http://www.362tq.com/jinhu/10tian.html 推荐查看天气:http://www.362tq.com/jinhu/15tian.html