Skip to content

Commit

Permalink
Forward all bundle URL params through HMR and bundle splitting
Browse files Browse the repository at this point in the history
Summary:
Forwards any extra parameters set on the main bundle URL (in development) to the derived bundle URLs used in hot reloading and bundle splitting.

Changelog: [General] - Forward URL parameters from main bundle to hot reloaded bundles

Reviewed By: cpojer

Differential Revision: D21455592

fbshipit-source-id: e1c375e3b6a0f3387372f1d96498dbf7d5237a09
  • Loading branch information
motiz88 authored and facebook-github-bot committed May 12, 2020
1 parent 9b53591 commit b4785e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Libraries/Core/Devtools/getDevServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import NativeSourceCode from '../../NativeModules/specs/NativeSourceCode';

let _cachedDevServerURL: ?string;
let _cachedFullBundleURL: ?string;
const FALLBACK = 'http://localhost:8081/';

type DevServerInfo = {
url: string,
fullBundleUrl: ?string,
bundleLoadedFromServer: boolean,
...
};
Expand All @@ -27,14 +29,15 @@ type DevServerInfo = {
*/
function getDevServer(): DevServerInfo {
if (_cachedDevServerURL === undefined) {
const match = NativeSourceCode.getConstants().scriptURL.match(
/^https?:\/\/.*?\//,
);
const scriptUrl = NativeSourceCode.getConstants().scriptURL;
const match = scriptUrl.match(/^https?:\/\/.*?\//);
_cachedDevServerURL = match ? match[0] : null;
_cachedFullBundleURL = match ? scriptUrl : null;
}

return {
url: _cachedDevServerURL || FALLBACK,
fullBundleUrl: _cachedFullBundleURL,
bundleLoadedFromServer: _cachedDevServerURL !== null,
};
}
Expand Down
10 changes: 9 additions & 1 deletion Libraries/Utilities/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const MetroHMRClient = require('metro/src/lib/bundle-modules/HMRClient');
const Platform = require('./Platform');
const prettyFormat = require('pretty-format');

import getDevServer from '../Core/Devtools/getDevServer';
import NativeRedBox from '../NativeModules/specs/NativeRedBox';
import * as LogBoxData from '../LogBox/Data/LogBoxData';
import type {ExtendedError} from '../Core/Devtools/parseErrorStack';
Expand Down Expand Up @@ -159,8 +160,15 @@ const HMRClient: HMRClientNativeInterface = {
const client = new MetroHMRClient(`ws://${wsHost}/hot`);
hmrClient = client;

const {fullBundleUrl} = getDevServer();
pendingEntryPoints.push(
`ws://${wsHost}/hot?bundleEntry=${bundleEntry}&platform=${platform}`,
// HMRServer understands regular bundle URLs, so prefer that in case
// there are any important URL parameters we can't reconstruct from
// `setup()`'s arguments.
fullBundleUrl ??
// The ws://.../hot?bundleEntry= format is an alternative to specifying
// a regular HTTP bundle URL.
`ws://${wsHost}/hot?bundleEntry=${bundleEntry}&platform=${platform}`,
);

client.on('connection-error', e => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"pretty-format": "^26.0.1",
"promise": "^8.0.3",
"prop-types": "^15.7.2",
"qs": "^6.5.1",
"react-devtools-core": "^4.6.0",
"react-refresh": "^0.4.0",
"regenerator-runtime": "^0.13.2",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6231,6 +6231,11 @@ punycode@^2.1.0, punycode@^2.1.1:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

qs@^6.5.1:
version "6.9.4"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.4.tgz#9090b290d1f91728d3c22e54843ca44aea5ab687"
integrity sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ==

qs@~6.5.2:
version "6.5.2"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
Expand Down

0 comments on commit b4785e5

Please sign in to comment.