【问题标题】:chrome devtools debug react, hit "refresh" cause debug stuckchrome devtools调试反应,点击“刷新”导致调试卡住
【发布时间】:2021-01-18 23:41:18
【问题描述】:

我不使用任何远程调试(webstorm 和 devtools)

只需使用 chrome devtools

当调试在 devtools 的断点处停止时

点击“ctrl+r”刷新也卡住了(选项卡空白,调试会话无法重新启动)

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8"/>
</head>
<body>
<script src="../node_modules/react/umd/react.development.js"></script>
<script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
<div id="root"></div>
<script>
  var e = React.createElement

  class App extends React.Component {
    render() {
      return e("div")
    }
  }

  const domContainer = document.querySelector('#root');
  ReactDOM.render(e(App), domContainer);
</script>
</body>
</html>

【问题讨论】:

  • 可能是开发工具中的一个错误。我通常先按 F8(取消暂停),然后按 Ctrl-R 或 F5。

标签: javascript reactjs google-chrome google-chrome-devtools


【解决方案1】:

更新:

这是我的解决方案:https://stackoverflow.com/a/64196599/6011193

更新: 我将此错误报告给 chrome 团队,请帮我投票:https://bugs.chromium.org/p/chromium/issues/detail?id=1134899#c_ts1603534836

旧: 我猜这个 chrome bug,所以这是我的 tmp 解决方案

简而言之,使用chrome扩展api“结束进程”当前标签进程然后重新加载,和手动chrome>任务管理器>结束进程然后点击重新加载是一样的

详细说明:

使用chrome.processes,它需要最新的chrome dev channel(不是chrome稳定版)

chrome.processes.terminate 终止当前选项卡进程

chrome.tabs.onUpdated监听“ctrl+r”重载事件,重载“localhost”dev url时,先“结束进程”再重载

以下是我的部分解决方案代码(完整代码很长,我省略了,但你可以参考自己编写代码)

/**
 *
 */
import {CrxAsync} from "./node_modules/ro-crx/src/index.js"

class ForceReloadInLocalhost extends CrxAsync {
  constructor() {
    super()

    chrome.processes.onExited.addListener(() => {
      this.startReload()
    })
  }

  start(tab) {
    if (tab == null) {
      this.getCurTab((curTab) => {
        this.start(curTab)
      })
    } else {
      this.tabId = tab.id;
      var tabId = this.tabId
      if (tab.url.match(/localhost/)) {
        this.killProcess(tabId, (status) => {
          if (status == "not_process") {
            this.startReload()
          }
        })
      } else {
        this.startReload()
      }
    }
  }

  startReload() {
    if (this.tabId) {
      this.reload(this.tabId, {}, () => {
        this.tabId = null
      })
    }
  }
}

var forceReload = new ForceReloadInLocalhost()

chrome.commands.onCommand.addListener((cmd) => {
  if (cmd == "forceReload") {
    forceReload.start()
  }
})

var isForceReload = true
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  if (tab.url.match(/^https?:\/\/localhost/)) {
    if (changeInfo.url == undefined /* it means is reload, so url doesn't change*/ && changeInfo.status == "loading") {
      if (!isForceReload) {
        isForceReload = true
        forceReload.start(tab);
      } else {
        isForceReload = false
      }
    }
  }
})

【讨论】:

    【解决方案2】:

    我发现this workaround 有类似问题:

    1. 尝试以隐身模式打开 devtools
    2. 在开发工具中选择另一个选项卡
    3. 关闭隐身窗口
    4. 再次定期尝试开发工具

    【讨论】:

      猜你喜欢
      • 2021-01-18
      • 2022-07-18
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2019-08-14
      • 2014-12-16
      • 1970-01-01
      • 2015-12-19
      相关资源
      最近更新 更多