Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/react-server/lib/dev/create-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,9 @@ export default async function createServer(root, options) {
return {
listen: (...args) => {
return viteDevServer.middlewares.listen(...args).once("listening", () => {
viteDevServer.environments.client.hot.listen();
if (config?.server?.hmr !== false) {
viteDevServer.environments.client.hot.listen();
}
});
},
close: () => {
Expand Down
15 changes: 9 additions & 6 deletions packages/react-server/lib/dev/ssr-handler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ export default async function ssrHandler(root) {
[MODULE_LOADER]: ssrLoadModule,
[IMPORT_MAP]: importMap,
[LOGGER_CONTEXT]: logger,
[MAIN_MODULE]: ["@vite/client", `@hmr`, `@__webpack_require__`].map(
(mod) =>
`${viteDevServer.config.base || "/"}/${mod}`.replace(
/\/+/g,
"/"
)
[MAIN_MODULE]: [
...(configRoot?.server?.hmr === false
? ["@__disable_hmr__"]
: []),
"@vite/client",
`@hmr`,
`@__webpack_require__`,
].map((mod) =>
`${viteDevServer.config.base || "/"}/${mod}`.replace(/\/+/g, "/")
),
...(configRoot.scrollRestoration
? {
Expand Down
43 changes: 43 additions & 0 deletions packages/react-server/lib/plugins/react-server-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,49 @@ export default function viteReactServerRuntime() {
}
return moduleCache.get(id);
};`;
} else if (id.endsWith("@__disable_hmr__")) {
return `(function () {
if (typeof WebSocket === 'undefined') return;
const Orig = WebSocket;
function isViteHmr(protocols) {
if (protocols === 'vite-hmr') return true;
if (Array.isArray(protocols) && protocols.indexOf('vite-hmr') !== -1) return true;
return false;
}
function InertSocket() {
// Matches the surface @vite/client touches: readyState, send, close,
// addEventListener, removeEventListener, onopen/onmessage/onerror/onclose.
const listeners = {};
this.readyState = 3; // CLOSED
this.url = '';
this.protocol = 'vite-hmr';
this.send = function () {};
this.close = function () {};
this.addEventListener = function (type, fn) {
(listeners[type] || (listeners[type] = [])).push(fn);
};
this.removeEventListener = function (type, fn) {
var l = listeners[type]; if (!l) return;
var i = l.indexOf(fn); if (i !== -1) l.splice(i, 1);
};
this.onopen = this.onmessage = this.onerror = this.onclose = null;
}
InertSocket.CONNECTING = 0;
InertSocket.OPEN = 1;
InertSocket.CLOSING = 2;
InertSocket.CLOSED = 3;

window.WebSocket = function (url, protocols) {
if (isViteHmr(protocols)) return new InertSocket();
return new Orig(url, protocols);
};
// Preserve statics so libraries that read WebSocket.OPEN etc. keep working.
window.WebSocket.CONNECTING = Orig.CONNECTING;
window.WebSocket.OPEN = Orig.OPEN;
window.WebSocket.CLOSING = Orig.CLOSING;
window.WebSocket.CLOSED = Orig.CLOSED;
window.WebSocket.prototype = Orig.prototype;
})();`;
}
},
},
Expand Down
Loading