Skip to content

Dev mode: reconnect the background's dev-server WebSocket after it closes #2707

Open

Description

@denkasyanov

Feature Request

In dev mode, the background script reconnects to the dev server after its WebSocket closes.

With webExt: { disabled: true } (Chrome ignores --load-extension, so the dev build is loaded unpacked by hand), the extension outlives each wxt dev run. getDevServerWebSocket in packages/wxt/src/utils/internal/dev-server-websocket.ts opens the socket once; on close it only logs "Disconnected from dev server", and ws keeps the closed socket, so the background never connects again. After a dev-server restart or a dropped socket, every later build reaches nobody, and hot reload stops until the extension is reloaded by hand at chrome://extensions.

Proposal: on close, the background probes the dev server every few seconds. When it answers, the background calls browser.runtime.reload(), and the fresh background reloads the tabs its content scripts match, because the reload cuts their content scripts off from the extension. We run this today as a patch of dist/virtual/background-entrypoint.mjs on 0.21.5 (about 30 lines, dev build only).

Is your feature request related to a bug?

N/A

What are the alternatives?

  • Reconnect the socket in place and attach the listeners again: builds made while the socket was down are lost, so the extension can run stale code until the next edit.
  • Reload the extension by hand after each dev-server restart: what users do today.

Additional context

wxt 0.21.5, Chrome on macOS. The main branch has the same dev-server-websocket.ts code.

Activity

  1. denkasyanov commented on Oct 31, 2026

    @denkasyanov
    Author

    One more detail from running the patch: the fresh background has to know that it should reload the tabs, so a flag has to survive browser.runtime.reload(). We keep it in browser.storage.local, which needs the storage permission; in an extension that does not ask for it, the write throws and the reload never happens. WXT already adds tabs and scripting to the dev manifest, so adding storage in dev too would keep the reconnect working for every extension.

  2. fkatsuhiro commented on Nov 5, 2026

    @fkatsuhiro
    Collaborator

    Hi, @denkasyanov san
    Thank you reporting! Is this issue is same as #1454 ?

    I've added workaround in documentation as tips. Could you take a look and see if it helps?
    https://wxt.dev/guide/essentials/config/browser-startup.html#disable-opening-browser

    Thanks!

  3. fkatsuhiro commented on Nov 14, 2026

    @fkatsuhiro
    Collaborator

    Let me investigate auto-connect feature solution. Thanks.

  4. denkasyanov commented on Nov 24, 2026

    @denkasyanov
    Author

    Thanks for looking into this! It's not the same: #1454 asked for docs on developing with or without the runner, and #2715 covers that. This issue asks for the background to reconnect by itself.

    The tip helps as a workaround, since Alt + R brings hot reload back without opening chrome://extensions. It still has to be pressed after every restart or dropped socket, and nothing shows that the extension has stopped receiving builds, so stale code is easy to miss.

    This is the reconnect part of the patch I run on 0.21.5, in dist/virtual/background-entrypoint.mjs:

    let reconnecting = false;
    function reloadWhenDevServerReturns() {
      if (reconnecting) return;
      reconnecting = true;
      const timer = setInterval(() => {
        const probe = new WebSocket(__DEV_SERVER_ORIGIN__, "vite-hmr");
        probe.addEventListener("open", () => {
          clearInterval(timer);
          probe.close();
          browser.runtime.reload();
        });
      }, 2e3);
    }
    
    // in the `import.meta.env.COMMAND === "serve"` block
    const ws = getDevServerWebSocket();
    ws.addEventListener("close", reloadWhenDevServerReturns);

    One correction to my earlier comment: the storage permission is not needed in MV3. After runtime.reload(), the fresh background sends wxt:background-initialized, and WXT then registers the content scripts and reloads their tabs.

    #2750 is the direction this issue asks for, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions