Skip to content

Commit 89edbcc

Browse files
fix: speed up initial client bundling
1 parent 4382bcb commit 89edbcc

File tree

2 files changed

+66
-73
lines changed

2 files changed

+66
-73
lines changed

client-src/index.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* global __resourceQuery, __webpack_hash__ */
22
/// <reference types="webpack/module" />
33
import webpackHotLog from "webpack/hot/log.js";
4+
import hotEmitter from "webpack/hot/emitter.js";
45
import socket from "./socket.js";
56
import { formatProblem, createOverlay } from "./overlay.js";
67
import { log, setLogLevel } from "./utils/log.js";
78
import sendMessage from "./utils/sendMessage.js";
8-
import reloadApp from "./utils/reloadApp.js";
99
import { isProgressSupported, defineProgressElement } from "./progress.js";
1010

1111
/**
@@ -249,6 +249,71 @@ const overlay =
249249
)
250250
: { send: () => {} };
251251

252+
/**
253+
* @param {Options} options
254+
* @param {Status} currentStatus
255+
*/
256+
const reloadApp = ({ hot, liveReload }, currentStatus) => {
257+
if (currentStatus.isUnloading) {
258+
return;
259+
}
260+
261+
const { currentHash, previousHash } = currentStatus;
262+
const isInitial =
263+
currentHash.indexOf(/** @type {string} */ (previousHash)) >= 0;
264+
265+
if (isInitial) {
266+
return;
267+
}
268+
269+
/**
270+
* @param {Window} rootWindow
271+
* @param {number} intervalId
272+
*/
273+
function applyReload(rootWindow, intervalId) {
274+
clearInterval(intervalId);
275+
276+
log.info("App updated. Reloading...");
277+
278+
rootWindow.location.reload();
279+
}
280+
281+
const search = self.location.search.toLowerCase();
282+
const allowToHot = search.indexOf("webpack-dev-server-hot=false") === -1;
283+
const allowToLiveReload =
284+
search.indexOf("webpack-dev-server-live-reload=false") === -1;
285+
286+
if (hot && allowToHot) {
287+
log.info("App hot update...");
288+
289+
hotEmitter.emit("webpackHotUpdate", currentStatus.currentHash);
290+
291+
if (typeof self !== "undefined" && self.window) {
292+
// broadcast update to window
293+
self.postMessage(`webpackHotUpdate${currentStatus.currentHash}`, "*");
294+
}
295+
}
296+
// allow refreshing the page only if liveReload isn't disabled
297+
else if (liveReload && allowToLiveReload) {
298+
let rootWindow = self;
299+
300+
// use parent window for reload (in case we're in an iframe with no valid src)
301+
const intervalId = self.setInterval(() => {
302+
if (rootWindow.location.protocol !== "about:") {
303+
// reload immediately if protocol is valid
304+
applyReload(rootWindow, intervalId);
305+
} else {
306+
rootWindow = rootWindow.parent;
307+
308+
if (rootWindow.parent === rootWindow) {
309+
// if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
310+
applyReload(rootWindow, intervalId);
311+
}
312+
}
313+
});
314+
}
315+
};
316+
252317
const ansiRegex = new RegExp(
253318
[
254319
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",

client-src/utils/reloadApp.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)