Skip to content

Comments

feat: support server custom message handling by module.hot.on#7003

Merged
chenjiahan merged 9 commits intomainfrom
module.hot.on
Jan 23, 2026
Merged

feat: support server custom message handling by module.hot.on#7003
chenjiahan merged 9 commits intomainfrom
module.hot.on

Conversation

@SyMind
Copy link
Member

@SyMind SyMind commented Jan 20, 2026

Summary

This PR improves the client-side HMR custom message handling by extracting the module.hot.on interception logic into a dedicated helper setupCustomHMRListeners.

Related Links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@netlify
Copy link

netlify bot commented Jan 20, 2026

Deploy Preview for rsbuild-v2 ready!

Name Link
🔨 Latest commit 8d4c3c8
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild-v2/deploys/696f5c047674080008628691
😎 Deploy Preview https://deploy-preview-7003--rsbuild-v2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@SyMind SyMind changed the title feat: support server custom message feat: support server custom message by module.hot.on Jan 20, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @SyMind, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the development server's capabilities by enabling it to send custom messages to the client-side HMR system. This feature allows for more flexible and powerful interactions between the server and client during development, facilitating custom HMR behaviors. The changes also include careful management of HMR event listeners to ensure they are correctly disposed of when modules are reloaded, preventing potential issues.

Highlights

  • Custom Server Messages: Implements a new mechanism for the development server to send custom messages to the client-side HMR runtime.
  • HMR Listener Management: Introduces a robust system to register and automatically dispose of custom HMR event listeners, preventing memory leaks on module reloads.
  • Rspack Runtime Integration: Adds a custom Rspack plugin to correctly inject HMR runtime globals (module.hot, RuntimeGlobals.interceptModuleExecution) into the bundled assets.
  • End-to-End Testing: Includes new e2e tests to validate the functionality of custom server messages and the proper disposal of HMR event callbacks.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@SyMind SyMind changed the title feat: support server custom message by module.hot.on feat: support server custom message handling by module.hot.on Jan 20, 2026
@SyMind SyMind marked this pull request as ready for review January 20, 2026 10:21
Copilot AI review requested due to automatic review settings January 20, 2026 10:21
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a great feature for sending custom messages from the dev server to the client using module.hot.on. The implementation is well-thought-out, especially the mechanism for disposing of HMR listeners to prevent leaks. The accompanying e2e tests cover the new functionality well. I've provided a few suggestions to improve the robustness and performance of the implementation, including a fix for a potential race condition in one of the new tests.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for server-side custom message handling through the HMR system, enabling developers to send custom events from the server to the client via module.hot.on. The implementation extracts custom message handling logic into a dedicated helper function.

Changes:

  • Added ServerCustomMessage type to support custom WebSocket messages
  • Implemented setupCustomHMRListeners to intercept and manage module.hot.on listeners with proper cleanup on module disposal
  • Added RspackRuntimeReplacePlugin to replace build-time constants with runtime values
  • Created comprehensive E2E tests verifying custom message handling and listener disposal

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
packages/core/src/server/socketServer.ts Adds ServerCustomMessage type definition to the message union
packages/core/src/client/hmr.ts Implements custom HMR listener setup with disposal logic and message handling
packages/core/rslib.config.ts Adds plugin for runtime constant replacement and reorganizes imports
e2e/cases/javascript-api/server-custom-message/src/index.js Test source file demonstrating custom event listener usage
e2e/cases/javascript-api/server-custom-message/index.test.ts E2E tests validating custom message delivery and listener cleanup

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 22, 2026

Deploying rsbuild-v2 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2efad7e
Status: ✅  Deploy successful!
Preview URL: https://cfe20187.rsbuild-v2.pages.dev
Branch Preview URL: https://module-hot-on.rsbuild-v2.pages.dev

View logs

Comment on lines +9 to +15
const rsbuild = await createRsbuild({
cwd: import.meta.dirname,
});

const server = await rsbuild.createDevServer();

await server.listen();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use the dev helper?

Copy link
Member Author

@SyMind SyMind Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot get dev server instance from dev() function.
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to update the helper

@cloudflare-workers-and-pages
Copy link

Deploying rsbuild with  Cloudflare Pages  Cloudflare Pages

Latest commit: d431722
Status: ✅  Deploy successful!
Preview URL: https://217fce2d.rsbuild-v2.pages.dev
Branch Preview URL: https://module-hot-on.rsbuild-v2.pages.dev

View logs

@chenjiahan chenjiahan merged commit 4c48709 into main Jan 23, 2026
6 checks passed
@chenjiahan chenjiahan deleted the module.hot.on branch January 23, 2026 13:09
@chenjiahan
Copy link
Member

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants