Search code examples
javascriptreactjsgoogle-chromegoogle-chrome-devtools

chrome devtools debug react, hit "refresh" cause debug stuck


I don't use any remote debug(webstorm and devtools)

just use chrome devtools

when debug stop in breakpoint in devtools

hit "ctrl+r" refresh also stuck(tab blank, debug session cannot restart)

enter image description here

<!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>

Solution

  • update:

    here is my solution: https://stackoverflow.com/a/64196599/6011193

    update: I report this bug to chrome team, please help me vote this issue: https://bugs.chromium.org/p/chromium/issues/detail?id=1134899#c_ts1603534836

    old: I guess this chrome bug, so here is my tmp solution

    in short, use chrome extension api "end process" current tab process and then reload, it's same manually chrome > task manager > end process and then hit reload

    in detail:

    use chrome.processes, it require latest chrome dev channel(not chrome stable version)

    chrome.processes.terminate to terminate current tab process

    chrome.tabs.onUpdated listen "ctrl+r" reload event, when reload "localhost" dev url, first "end process" and then reload

    following is my part solution code(full code is very long so I omit it, but you can refer to make yourself code)

    /**
     *
     */
    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
          }
        }
      }
    })