Skip to content

Commit 86b9414

Browse files
authored
fix: disabled hmr (#384)
Do not `viteDevServer.environments.client.hot.listen()` when HMR is disabled.
1 parent 70a320f commit 86b9414

3 files changed

Lines changed: 55 additions & 7 deletions

File tree

packages/react-server/lib/dev/create-server.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,9 @@ export default async function createServer(root, options) {
12001200
return {
12011201
listen: (...args) => {
12021202
return viteDevServer.middlewares.listen(...args).once("listening", () => {
1203-
viteDevServer.environments.client.hot.listen();
1203+
if (config?.server?.hmr !== false) {
1204+
viteDevServer.environments.client.hot.listen();
1205+
}
12041206
});
12051207
},
12061208
close: () => {

packages/react-server/lib/dev/ssr-handler.mjs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,15 @@ export default async function ssrHandler(root) {
9393
[MODULE_LOADER]: ssrLoadModule,
9494
[IMPORT_MAP]: importMap,
9595
[LOGGER_CONTEXT]: logger,
96-
[MAIN_MODULE]: ["@vite/client", `@hmr`, `@__webpack_require__`].map(
97-
(mod) =>
98-
`${viteDevServer.config.base || "/"}/${mod}`.replace(
99-
/\/+/g,
100-
"/"
101-
)
96+
[MAIN_MODULE]: [
97+
...(configRoot?.server?.hmr === false
98+
? ["@__disable_hmr__"]
99+
: []),
100+
"@vite/client",
101+
`@hmr`,
102+
`@__webpack_require__`,
103+
].map((mod) =>
104+
`${viteDevServer.config.base || "/"}/${mod}`.replace(/\/+/g, "/")
102105
),
103106
...(configRoot.scrollRestoration
104107
? {

packages/react-server/lib/plugins/react-server-runtime.mjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,49 @@ export default function viteReactServerRuntime() {
7373
}
7474
return moduleCache.get(id);
7575
};`;
76+
} else if (id.endsWith("@__disable_hmr__")) {
77+
return `(function () {
78+
if (typeof WebSocket === 'undefined') return;
79+
const Orig = WebSocket;
80+
function isViteHmr(protocols) {
81+
if (protocols === 'vite-hmr') return true;
82+
if (Array.isArray(protocols) && protocols.indexOf('vite-hmr') !== -1) return true;
83+
return false;
84+
}
85+
function InertSocket() {
86+
// Matches the surface @vite/client touches: readyState, send, close,
87+
// addEventListener, removeEventListener, onopen/onmessage/onerror/onclose.
88+
const listeners = {};
89+
this.readyState = 3; // CLOSED
90+
this.url = '';
91+
this.protocol = 'vite-hmr';
92+
this.send = function () {};
93+
this.close = function () {};
94+
this.addEventListener = function (type, fn) {
95+
(listeners[type] || (listeners[type] = [])).push(fn);
96+
};
97+
this.removeEventListener = function (type, fn) {
98+
var l = listeners[type]; if (!l) return;
99+
var i = l.indexOf(fn); if (i !== -1) l.splice(i, 1);
100+
};
101+
this.onopen = this.onmessage = this.onerror = this.onclose = null;
102+
}
103+
InertSocket.CONNECTING = 0;
104+
InertSocket.OPEN = 1;
105+
InertSocket.CLOSING = 2;
106+
InertSocket.CLOSED = 3;
107+
108+
window.WebSocket = function (url, protocols) {
109+
if (isViteHmr(protocols)) return new InertSocket();
110+
return new Orig(url, protocols);
111+
};
112+
// Preserve statics so libraries that read WebSocket.OPEN etc. keep working.
113+
window.WebSocket.CONNECTING = Orig.CONNECTING;
114+
window.WebSocket.OPEN = Orig.OPEN;
115+
window.WebSocket.CLOSING = Orig.CLOSING;
116+
window.WebSocket.CLOSED = Orig.CLOSED;
117+
window.WebSocket.prototype = Orig.prototype;
118+
})();`;
76119
}
77120
},
78121
},

0 commit comments

Comments
 (0)