-
-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content script is reloaded twice when the file is saved. #938
Comments
Can't reproduce: Videooutput.mp4But I think I can guess what the problem is. Can you apply this patch and share the logs? diff --git a/node_modules/wxt/dist/core/create-server.mjs b/node_modules/wxt/dist/core/create-server.mjs
index 30688b19352a3c5df0a99ae3526f92952d375cd3..7d3db3668d23d0cecdd2db550901185b5bb0c861 100644
--- a/node_modules/wxt/dist/core/create-server.mjs
+++ b/node_modules/wxt/dist/core/create-server.mjs
@@ -102,6 +102,7 @@ function createFileReloader(server) {
changeQueue.push([event, path]);
await fileChangedMutex.runExclusive(async () => {
if (server.currentOutput == null) return;
+ console.log(changeQueue);
const fileChanges = changeQueue.splice(0, changeQueue.length).map(([_, file]) => file);
if (fileChanges.length === 0) return;
await wxt.reloadConfig();
|
I applied the patch. wxt-video04.mp4I noticed that the Content script works correctly when I update the code using Notepad. wxt-video03.mp4 |
Hmm, not what I expected. It seems like VS Code is saving the file multiple times, and slow enough that my current debouncing logic isn't considering it a single save. On my side, the only thing I could do is increase the delay, or replace the current logic with a real throttle. But I haven't seen anyone else with this error, so I'd like to figure out why it's happening to you first. Do you have any extensions installed that would be causing this? Can you restart vscode and open it with extensions disabled to see if that's the problem? code --disable-extensions path/to/your-project |
I tried creating a new profile and disabling extensions, but the issue still persists. wxt-video05.Clipchamp.mp4 |
Nah, if I look closely at…it's a bit different from his. He has [ Edit: He also gets multiple browser alerts, but I only get 1 So I couldn't reproduce. |
@kazu-ux The difference between our environment and yours is that Windows/MacOS and you are using different disk drives And I found the Windows drive issue in Chokidar paulmillr/chokidar#665 - does the problem continue when you run WXT on the |
The same issue occurred on the C drive. |
@kazu-ux Okay, once you said that it does not reproduce in Notepad, is it 100% ? If so, we can say the problem isn't with chokidar. Because in the video you posted with the |
Yes, I edited the code 50 times in Notepad, but no issues occurred. |
@kazu-ux Thanks for many tried. So…it may as I thought, be a VSCode issue or that with the VSCode's integrated terminal. We can only suspect VSCode settings, but what happens if you try set You could add the below code to {
"files.watcherExclude": {
"**/.output/**": true
},
"typescript.tsserver.experimental.useVsCodeWatcher": false
} I can only think that something primitive in VSCode is the cause. |
I changed settings. but the issue still persists. I noticed that it works properly when I edit the popup code. wxt-video06.Clipchamp.mp4 |
So weird, reloading popup and content-script have different methods, but I don't know why it would make a difference with that and the editor. 🤷 |
Even if we can't reproduce, the issue is quite clear. There's a couple of solutions:
Thoughts on option 2? That's my preferred solution |
@aklinker1 Yes, I think option 2 is the best if we try to tune it up. Chokidar has many performance options, but too complicated lol. https://github.com/paulmillr/chokidar#performance Do we use |
Didn't know about that option... Could be a solution as well. The other option is to wait for the v4 rewrite they're in the middle of. You can already pass in the watcher options via vite - WXT doesn't have its own file watcher |
Actually, that might be wrong. I need to double check the code |
Yep, we can actually pass it via vite config. But currently the user side values isn't merged with the default WXT side values in
I was thinking too. I just don't know if v4 will change the performance aspect. Looking at the current v4 branch, it looks like the main focus is TSing and getting rid of dependencies. And vite is discussing about watcher with chokidar for a long time lol. vitejs/vite#13593 |
Maybe we should follow the chokidar v4 and vite movement and only ready the stable passing watcher option in place until then. |
Yeah, I'm good with that. For now, let's just add debouncing |
Let's do so. I can work on it, but do you have any particulars about the debounce library? |
@1natsu172 thanks! I prefer unjs packages: https://unjs.io/packages/perfect-debounce But if there's already another as a sub dependency, we can use that one too |
@kazu-ux We are working on PR for this issue. For reference, we would like to know how many seconds the reload actually takes, so please help us out. The following patch will give you the seconds for reloading. diff --git a/packages/wxt/dist/core/create-server.mjs b/packages/wxt/dist/core/create-server.mjs
index d74a536..239f099 100644
--- a/packages/wxt/dist/core/create-server.mjs
+++ b/packages/wxt/dist/core/create-server.mjs
@@ -19,6 +19,7 @@ import {
getContentScriptJs,
mapWxtOptionsToRegisteredContentScript
} from "./utils/content-scripts.mjs";
+import { formatDuration } from "./utils/time.mjs";
export async function createServer(inlineConfig) {
await registerWxt("serve", inlineConfig, async (config) => {
const { port, hostname } = config.dev.server;
@@ -92,7 +93,16 @@ export async function createServer(inlineConfig) {
reloadContentScripts(server.currentOutput.steps, server);
});
const reloadOnChange = createFileReloader(server);
- server.watcher.on("all", reloadOnChange);
+ let prev = Date.now();
+ const timerlog = () => {
+ const now = Date.now();
+ console.log(formatDuration(now - prev));
+ prev = now;
+ };
+ server.watcher.on("all", (...args) => {
+ timerlog();
+ reloadOnChange(...args);
+ });
return server;
}
function createFileReloader(server) { |
|
I patched the code. |
Chokidor v4 just got dropped: https://github.com/paulmillr/chokidar/releases/tag/4.0.0 But we'll probably have to wait for Vite 6 before using it |
I updated wxt to 0.19.10, the issue was resolved. Thank you! |
@kazu-ux Oh sorry, I forgot to tell you that it's released. Glad to hear it has been resolved! (cc: @pnd280). Can adjust it by environment variable.
|
Gonna go ahead and close this as completed! |
Describe the bug
Content script is reloaded twice when the file is saved.
Reproduction
wxt-video01.mp4
Steps to reproduce
Run
npm install
followed bynpm run dev
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: