reloadApp.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import hotEmitter from "webpack/hot/emitter.js";
  2. import { log } from "./log.js";
  3. /** @typedef {import("../index").Options} Options
  4. /** @typedef {import("../index").Status} Status
  5. /**
  6. * @param {Options} options
  7. * @param {Status} status
  8. */
  9. function reloadApp(_ref, status) {
  10. var hot = _ref.hot,
  11. liveReload = _ref.liveReload;
  12. if (status.isUnloading) {
  13. return;
  14. }
  15. var currentHash = status.currentHash,
  16. previousHash = status.previousHash;
  17. var isInitial = currentHash.indexOf(
  18. /** @type {string} */
  19. previousHash) >= 0;
  20. if (isInitial) {
  21. return;
  22. }
  23. /**
  24. * @param {Window} rootWindow
  25. * @param {number} intervalId
  26. */
  27. function applyReload(rootWindow, intervalId) {
  28. clearInterval(intervalId);
  29. log.info("App updated. Reloading...");
  30. rootWindow.location.reload();
  31. }
  32. var search = self.location.search.toLowerCase();
  33. var allowToHot = search.indexOf("webpack-dev-server-hot=false") === -1;
  34. var allowToLiveReload = search.indexOf("webpack-dev-server-live-reload=false") === -1;
  35. if (hot && allowToHot) {
  36. log.info("App hot update...");
  37. hotEmitter.emit("webpackHotUpdate", status.currentHash);
  38. if (typeof self !== "undefined" && self.window) {
  39. // broadcast update to window
  40. self.postMessage("webpackHotUpdate".concat(status.currentHash), "*");
  41. }
  42. } // allow refreshing the page only if liveReload isn't disabled
  43. else if (liveReload && allowToLiveReload) {
  44. var rootWindow = self; // use parent window for reload (in case we're in an iframe with no valid src)
  45. var intervalId = self.setInterval(function () {
  46. if (rootWindow.location.protocol !== "about:") {
  47. // reload immediately if protocol is valid
  48. applyReload(rootWindow, intervalId);
  49. } else {
  50. rootWindow = rootWindow.parent;
  51. if (rootWindow.parent === rootWindow) {
  52. // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
  53. applyReload(rootWindow, intervalId);
  54. }
  55. }
  56. });
  57. }
  58. }
  59. export default reloadApp;