chrome-extension-tools icon indicating copy to clipboard operation
chrome-extension-tools copied to clipboard

HMR doesn't work

Open Kurohyou opened this issue 11 months ago • 9 comments

Build tool

Vite

Where do you see the problem?

  • [X] In the browser
  • [ ] In the terminal

Describe the bug

Following the Vite 3 instructions, HMR does not work. In the popup's console, I see the following error:

client.ts:77 WebSocket connection to 'ws://localhost/' failed: 
setupWebSocket @ client.ts:77
Show 1 more frame
client.ts:77 Uncaught (in promise) DOMException: Failed to construct 'WebSocket': The URL 'ws://localhost:undefined/' is invalid.
    at setupWebSocket (chrome-extension://papdbjhkiieikcdobehdenilkjknhjfo/@vite/client:245:20)
    at fallback (chrome-extension://papdbjhkiieikcdobehdenilkjknhjfo/@vite/client:224:22)
    at WebSocket.<anonymous> (chrome-extension://papdbjhkiieikcdobehdenilkjknhjfo/@vite/client:259:13)
setupWebSocket @ client.ts:77
fallback @ client.ts:42
(anonymous) @ client.ts:98
Show 3 more frames

Reproduction

Logs

client-worker:35 [vite] connecting...
client-worker:50 [vite] connected.
client.ts:18 [vite] connecting...
DevTools failed to load source map: Could not load content for chrome-extension://papdbjhkiieikcdobehdenilkjknhjfo/node_modules/.vite/deps/vue.js.map: System error: net::ERR_FILE_NOT_FOUND
client.ts:77 WebSocket connection to 'ws://localhost/' failed: 
setupWebSocket @ client.ts:77
Show 1 more frame
client.ts:77 Uncaught (in promise) DOMException: Failed to construct 'WebSocket': The URL 'ws://localhost:undefined/' is invalid.
    at setupWebSocket (chrome-extension://papdbjhkiieikcdobehdenilkjknhjfo/@vite/client:245:20)
    at fallback (chrome-extension://papdbjhkiieikcdobehdenilkjknhjfo/@vite/client:224:22)
    at WebSocket.<anonymous> (chrome-extension://papdbjhkiieikcdobehdenilkjknhjfo/@vite/client:259:13)
setupWebSocket @ client.ts:77
fallback @ client.ts:42
(anonymous) @ client.ts:98
Show 3 more frames

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 2.13 GB / 15.68 GB
  Binaries:
    Node: 18.4.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.12.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1848.0), Chromium (114.0.1823.67)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @crxjs/vite-plugin: ^2.0.0-beta.18 => 2.0.0-beta.18
    vite: ^4.3.9 => 4.3.9

Severity

annoyance

Kurohyou avatar Jul 03 '23 15:07 Kurohyou

I ran into a similar issue using vite@4. My solution was to define the port in vite.config.ts. See https://vitejs.dev/config/server-options.html#server-port

e.g.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import manifest from "./manifest.json";
import { crx } from "@crxjs/vite-plugin";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), crx({ manifest })],
  server: {
    port: 3000,
  },
});

bobmccracken avatar Jul 15 '23 23:07 bobmccracken

I ran into a similar issue using vite@4. My solution was to define the port in vite.config.ts. See https://vitejs.dev/config/server-options.html#server-port

e.g.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import manifest from "./manifest.json";
import { crx } from "@crxjs/vite-plugin";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), crx({ manifest })],
  server: {
    port: 3000,
  },
});

It doesn't work.

molvqingtai avatar Jul 17 '23 03:07 molvqingtai

I ran into a similar issue using vite@4. My solution was to define the port in vite.config.ts. See https://vitejs.dev/config/server-options.html#server-port

e.g.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import manifest from "./manifest.json";
import { crx } from "@crxjs/vite-plugin";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), crx({ manifest })],
  server: {
    port: 3000,
  },
});

I needed to specify the same server port that the client is using which is in my case was 5173 not 3000 and it worked, thanks!

It seems the issue was that the service-worker once got triggered by HMR didn't know which port to reload. If you go directly to the port instead of the popup, HMR worked normally.

code-vagabond avatar Jul 17 '23 17:07 code-vagabond

It works for me!

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {crx} from '@crxjs/vite-plugin'
import manifest from './manifest.json' assert {type: 'json'} // Node >=17

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        crx({manifest}),
    ],
    server: {
        strictPort: true,
        port: 5173,
        hmr: {
            clientPort: 5173
        }
    },
})


xiaoyvyv avatar Jul 24 '23 08:07 xiaoyvyv

It works for me!

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {crx} from '@crxjs/vite-plugin'
import manifest from './manifest.json' assert {type: 'json'} // Node >=17

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        crx({manifest}),
    ],
    server: {
        strictPort: true,
        port: 5173,
        hmr: {
            clientPort: 5173
        }
    },
})

thanks, it helped

wokalek avatar Jul 28 '23 21:07 wokalek

Will this be fixed?

wong2 avatar Sep 11 '23 06:09 wong2

Yes, setting a fixed port for hmr and server is the solution.

mubaidr avatar Sep 27 '23 17:09 mubaidr

It's weird, setting the ports worked for a little bit for me, but then stopped working again. The service worker doesn't seem to reload.

clickblipclick avatar Oct 27 '23 16:10 clickblipclick