-
Has anyone gotten HMR, or even just a full page reload, working with content scripts? This is my vite config:
I've tried running both vite dev and vite build in watch mode, no luck. Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Edit: oh, you've disabled auto-launch. The response below assumes you're letting the plugin open the browser, which is what enables the automatic reloads on save, causing the runtime ID to be null, and thus making the hack work. With auto-launch disabled, the plugin is unable to tell the extension to reload. HMR won't work with content scripts since they are bundled into a single file in dev mode, just like they are in production. Also, content scripts don't support ES Modules, which HMR is built around.
As for reloading the page, you have to reload it yourself on Chrome after you save a file and the extension reloads. If you develop against Firefox, Firefox will reload the page for you automatically. That said, if you want a simple hack to enable this for Chrome, you can add the following code to your content script: if (import.meta.env.DEV) {
setInterval(() => {
// or chrome.runtime.id if you're not using webextension-polyfill
if (browser.runtime.id == null) location.reload()
}, 1000)
} This checks if the runtime ID is |
Beta Was this translation helpful? Give feedback.
Edit: oh, you've disabled auto-launch. The response below assumes you're letting the plugin open the browser, which is what enables the automatic reloads on save, causing the runtime ID to be null, and thus making the hack work.
With auto-launch disabled, the plugin is unable to tell the extension to reload.
HMR won't work with content scripts since they are bundled into a single file in dev mode, just like they are in production. Also, content scripts don't support ES Modules, which HMR is built around.