Skip to content

Commit a9007ea

Browse files
jbromafacebook-github-bot
authored andcommitted
feat: process HMR registerBundle calls from same origin only (#51821)
Summary: Part of facebook/metro#1480 This PR adds a check in `HMRClient.js` that prevents processing `registerBundle` calls coming from different origin than the one declared in HMR `setup()`. This is useful in a Module Federation setup, where we have multiple HMRClients present in runtime - when Host loads external remote, the requestURL will have different origin, but it will be processed by the HMRClient from the Host which in turn causes a runtime error. ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [GENERAL] [ADDED] - Process HMR registerBundle calls from the same origin only Pull Request resolved: #51821 Test Plan: TBD Reviewed By: christophpurrer Differential Revision: D76044353 Pulled By: huntie fbshipit-source-id: 3928347b1e9a90355d02b87b07fde812479bcb67
1 parent e17eab4 commit a9007ea

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/react-native/Libraries/Utilities/HMRClient.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const prettyFormat = require('pretty-format');
2323
const pendingEntryPoints = [];
2424
let hmrClient = null;
2525
let hmrUnavailableReason: string | null = null;
26+
let hmrOrigin: string | null = null;
2627
let currentCompileErrorMessage: string | null = null;
2728
let didConnect: boolean = false;
2829
let pendingLogs: Array<[LogLevel, $ReadOnlyArray<mixed>]> = [];
@@ -100,7 +101,14 @@ const HMRClient: HMRClientNativeInterface = {
100101
},
101102

102103
registerBundle(requestUrl: string) {
103-
invariant(hmrClient, 'Expected HMRClient.setup() call at startup.');
104+
invariant(
105+
hmrOrigin != null && hmrClient != null,
106+
'Expected HMRClient.setup() call at startup.',
107+
);
108+
// only process registerBundle calls from the same origin
109+
if (!requestUrl.startsWith(hmrOrigin)) {
110+
return;
111+
}
104112
pendingEntryPoints.push(requestUrl);
105113
registerBundleEntryPoints(hmrClient);
106114
},
@@ -162,8 +170,10 @@ const HMRClient: HMRClientNativeInterface = {
162170

163171
const serverScheme = scheme;
164172

165-
const client = new MetroHMRClient(`${serverScheme}://${serverHost}/hot`);
173+
const origin = `${serverScheme}://${serverHost}`;
174+
const client = new MetroHMRClient(`${origin}/hot`);
166175

176+
hmrOrigin = origin;
167177
hmrClient = client;
168178

169179
const {fullBundleUrl} = getDevServer();

0 commit comments

Comments
 (0)