close
logologo
Guide
Config
Plugin
API
Community
Version
Changelog
Rsbuild 0.x Doc
English
简体中文
Guide
Config
Plugin
API
Community
Changelog
Rsbuild 0.x Doc
English
简体中文
logologo

Getting Started

Introduction
Quick start
Features
Glossary

Framework

React
Vue
Preact
Svelte
Solid

Basic

CLI
Dev server
Output files
Static assets
HTML
JSON
Wasm
TypeScript
Web Workers
Deploy static site
Upgrade Rsbuild

Configuration

Configure Rspack
Configure Rsbuild
Configure SWC

Styling

CSS
CSS Modules
CSS-in-JS
Tailwind CSS v4
Tailwind CSS v3
UnoCSS

Advanced

Path aliases
Environment variables
Hot module replacement
Browserslist
Browser compatibility
Module Federation
Multi-environment builds
Server-side rendering (SSR)
Testing

Optimization

Code splitting
Bundle size optimization
Improve build performance
Inline static assets

Migration

Migrating from Rsbuild 0.x
webpack
Create React App
Vue CLI
Vite
Vite plugin
Modern.js Builder

Debug

Debug mode
Build profiling
Use Rsdoctor

FAQ

General FAQ
Features FAQ
Exceptions FAQ
HMR FAQ
📝 Edit this page on GitHub
Previous PageExceptions FAQ

#HMR FAQ

#How to troubleshoot HMR issues?

There are several possible reasons why HMR may not work. This document covers the most common causes and provides troubleshooting guidance.

Before troubleshooting, it's helpful to understand how HMR works:

HMR principle
  1. The browser establishes a WebSocket connection with the dev server for real-time communication.
  2. When the dev server finishes recompiling, it sends a notification to the browser via WebSocket. The browser then sends a hot-update.(js|json) request to the dev server to load the newly compiled module.
  3. After receiving the new module, React projects use React Refresh (an official React tool) to update components. Other frameworks have similar tools.

After understanding how HMR works, you can follow these troubleshooting steps:

#1. Check the WebSocket connection

Open the browser console and check for the presence of the [HMR] connected. log.

  • If present, the WebSocket connection is working correctly. Continue with the following steps.
  • If not present, open the Network panel in Chrome and check the status of the ws://[host]:[port]/rsbuild-hmr request. If the request failed, this indicates that HMR failed because the WebSocket connection was not established.

The WebSocket connection can fail for various reasons, such as a network proxy preventing the WebSocket request from reaching the dev server. Check whether the WebSocket request address matches your dev server address. If it doesn't match, configure the WebSocket request address using dev.client.

#2. Check the hot-update requests

When you modify a module's code and trigger a recompilation, the browser sends several hot-update.json and hot-update.js requests to the dev server to fetch the updated code.

Try modifying a module and inspect the content of the hot-update.(js|json) requests. If the request contains the latest code, the hot update request is working correctly.

If the request content is incorrect, it's likely due to a network proxy. Check whether the hot-update.(js|json) request address matches your dev server address. If it doesn't match, adjust the proxy rules to route the hot-update.(js|json) requests to the dev server address.

#3. Check for other causes

If the above steps don't reveal any issues, other factors may be causing HMR to fail. For example, the code may not meet React's HMR requirements. Refer to the following questions for further troubleshooting.


#HMR not working with external React?

To ensure HMR works properly, you need to use the development builds of React and ReactDOM.

If you exclude React via externals during bundling, the production build of React is typically injected through a CDN, which can cause HMR to fail.

export default {
  output: {
    externals: {
      react: 'React',
      'react-dom': 'ReactDOM',
    },
  },
};

To solve this problem, reference the React development builds and install React DevTools. Hot reloading will then work properly.

If you're unsure which React build you're using, refer to the React documentation - Use the Production Build.


#HMR not working with filename hash in development mode?

Typically, filename hashes should only be set in production mode (when process.env.NODE_ENV === 'production').

Setting filename hashes in development mode can cause HMR to fail, especially for CSS files. This is because the hash changes every time the file content changes, preventing tools like mini-css-extract-plugin from reading the latest file content.

  • Correct usage:
export default {
  output: {
    filename: {
      css:
        process.env.NODE_ENV === 'production'
          ? '[name].[contenthash:8].css'
          : '[name].css',
    },
  },
};
  • Incorrect usage:
export default {
  output: {
    filename: {
      css: '[name].[contenthash:8].css',
    },
  },
};

#HMR not working with HTTPS?

When HTTPS is enabled, the HMR connection may fail due to certificate issues. If you open the console, you'll see an HMR connection failed error.

» WebSocket connection to 'wss://localhost:3000/rsbuild-hmr' failed:
[HMR] disconnected. Attempting to reconnect.

To solve this problem, click "Advanced" -> "Proceed to [domain] (unsafe)" in the Chrome warning page.

Tip: When accessing the page via localhost, the "Your connection is not private" warning may not appear. In that case, access the page via a network domain instead.