isaurssaurav / hot-reload-extension-vite-plugin

Simple vite plugin to reload chrome extension on file change.
MIT License
21 stars 4 forks source link

The socket connection does not automatically reconnect when disconnected. #9

Open woog2roid opened 3 months ago

woog2roid commented 3 months ago

It seems that there is no feature to reconnect in the package's src/scripts/background-reload.ts.

import { HOT_RELOAD_EXTENSION_VITE_PORT, Message } from '../utils';
/**
 * If development, this code will be appended to background script file.
 */
const socket = new WebSocket(`ws://localhost:${HOT_RELOAD_EXTENSION_VITE_PORT}`);

// No to let extension go to inactive state
const keepAlive = () => setInterval(chrome.runtime.getPlatformInfo, 20e3);
chrome.runtime.onStartup.addListener(keepAlive);
keepAlive();

chrome.runtime.onInstalled.addListener((details) => {
  if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
    chrome.tabs.reload();
  }
});

socket.addEventListener('message', (event) => {
  if (event.data === Message.FILE_CHANGE) {
    chrome.runtime.reload();
  }
});

It would be better to add code that reconnects using a polling method, and to allow this feature to be enabled from vite.config.ts with the default set to false. This is because continuously attempting to reconnect every time the browser is opened can burden computer resources, such as memory.

Then, it will look like this.

 hotReloadExtension({
        log: true,
        backgroundPath: 'src/pages/background/index.ts',
        autoReconnect: true,
        reconnectInterval: 10, // The polling interval is 10 seconds.
      }),

The addition of this feature will allow for a very convenient DX as you can turn the function on and off as needed.

isaurssaurav commented 2 months ago

This is because continuously attempting to reconnect every time the browser is opened can burden computer resources, such as memory.

Could you elaborate on burden computer resources part a bit and also give me brief about the issue you faced or we could face?

woog2roid commented 2 months ago

I think the extension needs to do polling the socket room for the reconnection feature. However, this will continue to send requests in the background, which I think could be a waste of computer resources. That's why I suggested an option like reconnectInterval.

isaurssaurav commented 2 months ago

Can you provide me some code example to resolve it ( as I am still not sure about you request)? or maybe open PR so that we both can look into it together?