Skip to content

Popup HTML auto refresh does not work with Vite 6.0.9+ #1360

Description

@sapphi-red

Describe the bug

Due to a security issue (GHSA-vg6x-rcgg-rjx6), Vite 6.0.9+ now requires a token to connect to the WebSocket endpoint.
Because WXT connects to the WS server on its own, this connection no longer successes.

const serverUrl = `${__DEV_SERVER_PROTOCOL__}//${__DEV_SERVER_HOSTNAME__}:${__DEV_SERVER_PORT__}`;
logger.debug('Connecting to dev server @', serverUrl);
ws = new WebSocket(serverUrl, 'vite-hmr') as WxtWebSocket;

The way to fix would be to pass config.webSocketToken as a query param (e.g. ?token=${config.webSocketToken}) when connecting to WS server.
https://github.com/vitejs/vite/blob/9654348258eaa0883171533a2b74b4e2825f5fb6/packages/vite/src/node/config.ts#L610-L620
But ideally, it should not connect to the WebSocket server on its own and instead use import.meta.hot.

Reproduction

pnpm dlx wxt@latest init

Steps to reproduce

  1. pnpm dlx wxt@latest init
  2. pnpm dev
  3. Open the extension popup.
  4. Edit entrypoints/popup/index.html

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 11.03 GB / 31.92 GB
  Binaries:
    Node: 22.13.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.2 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.15.3 - C:\Program Files\nodejs\pnpm.CMD
    bun: 1.1.38 - ~\AppData\Local\Microsoft\WinGet\Links\bun.EXE
  Browsers:
    Edge: Chromium (131.0.2903.99)
    Internet Explorer: 11.0.22621.3527

Used Package Manager

pnpm

Validations

Activity

  1. added
    pending-triage
    on Jan 21, 2025
  2. aklinker1 commented on Jan 21, 2025

    @aklinker1
    Member

    Thanks for the report, will get this fixed. Not sure why I didn't use the HMR API for this originally lol.


    For my reference, custom events using import.meta.hot:

    server.ws.send({
      type: 'custom',
      event: 'special-update',
      data: {}
    })
    if (import.meta.hot) {
      import.meta.hot.on('special-update', (data) => {
        // perform custom update
      })
    }
  3. added and removed
    pending-triage
    on Jan 21, 2025
  4. aklinker1 commented on Jan 21, 2025

    @aklinker1
    Member

    Hmm, yeah, so this doesn't work because import.meta.hot is not defined in the background. The background is not built as an ES module and doesn't include vite/client. If I can include it somehow, that'd be great. Otherwise, we'll have to pass a key manually like you mentioned.

  5. sapphi-red commented on Jan 21, 2025

    @sapphi-red
    Author

    Maybe you can build it as an ES Module and dynamically import it from a classic script?
    For manifest v2, adding import('http://localhost:3000/a.js') to .output/chrome-mv2/background.js and placing .output/chrome-mv2/a.js with import 'http://localhost:3000/@vite/client'; console.log('a', import.meta.url) worked.
    For manifest v3, I didn't try it but a dynamic import will likely work as well.

  6. aklinker1 commented on Jan 21, 2025

    @aklinker1
    Member

    Yeah, dynamic importing from the dev server might work. Will give it a go in a bit.

  7. aklinker1 commented on Jan 22, 2025

    @aklinker1
    Member

    Looks like non-ESM background scripts can't use dynamic imports:

    Image

    I got far enough on the PR setting this up that I'm blocked by this. Since WXT defaults to using non-module background scripts, this is not something WXT can fully adopt until it moves away from that.

    Additionally, even if you change your background to type: "module", the vite client tries to use the window variable, which isn't in service workers.

    Image

    So no, WXT cannot use the HMR API, and must connect to the websocket manually.

  8. aklinker1 commented on Jan 22, 2025

    @aklinker1
    Member

    WAIT, tried one last thing and I got something working:

    Image
  9. aklinker1 commented on Jan 22, 2025

    @aklinker1
    Member

    Nope, that only works for ESM background scripts. Gonna have to stick with connecting to the websocket manually.

  10. aklinker1 commented on Feb 8, 2025

    @aklinker1
    Member

    Had some time to think more about this... I'm gonna make a breaking change in a patch version update (similar to what Vite did, since this is security related): always set background.type: "module" in the dev manifest. This will allow me to use Vite's builtin import.meta.hot for communication.

    I will still respect the background's type: "module" option in terms of which type of build to use for the background (IIFE or ESM), but I will configure dev mode to always set "background.type": "module" in the manifest so it can use import(...). That way we can always access the vite dev server.

    This is my top priority, I've got a long flight home tomorrow, will try and get this done then.

  11. aklinker1 commented on Mar 4, 2025

    @aklinker1
    Member

    I released a workaround for this in v0.19.28. It doesn't solve the security issue, but it does allow you to upgrade to newer versions of Vite. Since as I haven't been able to work on and merge #1411 in a few weeks, I wanted to get something out to unblock people.

  12. added theissue type on Mar 21, 2025
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions