Skip to content

Content script is reloaded twice when the file is saved. #938

Closed

Description

@kazu-ux

Describe the bug

Content script is reloaded twice when the file is saved.

Reproduction

wxt-video01.mp4

Steps to reproduce

Run npm install followed by npm run dev

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 AMD Ryzen 5 3400G with Radeon Vega Graphics 

    Memory: 1.61 GB / 13.94 GB
  Binaries:
    Node: 20.11.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.22 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.9.0 - C:\Program Files\nodejs\pnpm.CMD
    bun: 1.1.10 - ~\AppData\Local\reflex\bun\bin\bun.EXE     
    Watchman: 20230806.083715.0 - D:\watchman\bin\watchman.EXE
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    wxt: ^0.19.7 => 0.19.7

Used Package Manager

npm

Validations

Activity

  1. added
    pending-triage
    on Aug 28, 2024
  2. aklinker1 commented on Aug 29, 2024

    @aklinker1
    Member

    Can't reproduce:

    Video
    output.mp4

    But 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();
    
  3. added and removed
    pending-triage
    on Aug 29, 2024
  4. kazu-ux commented on Aug 30, 2024

    @kazu-ux
    Author

    I applied the patch.

    wxt-video04.mp4

    I noticed that the Content script works correctly when I update the code using Notepad.

    wxt-video03.mp4
  5. aklinker1 commented on Aug 30, 2024

    @aklinker1
    Member

    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
  6. kazu-ux commented on Aug 30, 2024

    @kazu-ux
    Author

    I tried creating a new profile and disabling extensions, but the issue still persists.

    wxt-video05.Clipchamp.mp4
  7. 1natsu172 commented on Aug 30, 2024

    @1natsu172
    Contributor

    Hmm, I was able to reproduce this. Sometimes I got multiple “changed” logs. It happens sometimes when saved(⌘+S) once every 3sec by feel. Doesn't seem to be caused by personal something tool, it occurs in both npm and pnpm with VSCode and TextEdit.app
    image

  8. 1natsu172 commented on Aug 30, 2024

    @1natsu172
    Contributor

    Nah, if I look closely at…it's a bit different from his. He has [Changed: -> Reloaded:] x3, but I have multiple Changed: and Reloaded: is not shown after that. 🤷

    Edit: He also gets multiple browser alerts, but I only get 1 So I couldn't reproduce.

  9. 1natsu172 commented on Aug 31, 2024

    @1natsu172
    Contributor

    @kazu-ux The difference between our environment and yours is that Windows/MacOS and you are using different disk drives C:&D: (I noticed it).

    And I found the Windows drive issue in Chokidar paulmillr/chokidar#665 - does the problem continue when you run WXT on the C: drive?

  10. kazu-ux commented on Sep 7, 2024

    @kazu-ux
    Author

    The same issue occurred on the C drive.

  11. 1natsu172 commented on Sep 7, 2024

    @1natsu172
    Contributor

    @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 code --disable-extensions, the problem isn't happening at “Hello content. 2” and the timing is causing the problem at “Hello content. 3”. So the problem may in fact occur with Notepad if you try it many times.

  12. kazu-ux commented on Sep 7, 2024

    @kazu-ux
    Author

    Yes, I edited the code 50 times in Notepad, but no issues occurred.

  13. 1natsu172 commented on Sep 7, 2024

    @1natsu172
    Contributor

    @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 .output dir to the Files: Watcher Exclude and disable to TypeScript > TsServer > Experimental: Use Vs Code Watcher with workspace settings?

    You could add the below code to .vscode/settings.json, or set up in the GUI.

    {
      "files.watcherExclude": {
        "**/.output/**": true
      },
      "typescript.tsserver.experimental.useVsCodeWatcher": false
    }

    I can only think that something primitive in VSCode is the cause.

  14. kazu-ux commented on Sep 9, 2024

    @kazu-ux
    Author

    I changed settings. but the issue still persists.

    I noticed that it works properly when I edit the popup code.

    wxt-video06.Clipchamp.mp4
  15. 6 remaining items

  16. 1natsu172 commented on Sep 12, 2024

    @1natsu172
    Contributor

    Maybe we should follow the chokidar v4 and vite movement and only ready the stable passing watcher option in place until then.

  17. aklinker1 commented on Sep 12, 2024

    @aklinker1
    Member

    Yeah, I'm good with that. For now, let's just add debouncing

  18. 1natsu172 commented on Sep 12, 2024

    @1natsu172
    Contributor

    let's just add debouncing

    Let's do so. I can work on it, but do you have any particulars about the debounce library?

  19. aklinker1 commented on Sep 12, 2024

    @aklinker1
    Member

    @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

  20. 1natsu172 commented on Sep 14, 2024

    @1natsu172
    Contributor

    @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) {
  21. pnd280 commented on Sep 14, 2024

    @pnd280

    @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) {

    image
    Highlighted regions are double reloads
    btw, disable trailing seems to fix the issue 21446b1

  22. kazu-ux commented on Sep 14, 2024

    @kazu-ux
    Author

    @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) {
    image

    I patched the code.

  23. 1natsu172 commented on Sep 14, 2024

    @1natsu172
    Contributor

    @kazu-ux @pnd280 Thanks both. Hmm It is taking longer than thought. About 800ms max.... I found it helpful. I'll keep you posted on any progress!

  24. aklinker1 commented on Sep 18, 2024

    @aklinker1
    Member

    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

  25. kazu-ux commented on Sep 24, 2024

    @kazu-ux
    Author

    I updated wxt to 0.19.10, the issue was resolved. Thank you!

  26. 1natsu172 commented on Sep 24, 2024

    @1natsu172
    Contributor

    @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.

    wxt/packages/wxt/src/types.ts

    Lines 1280 to 1298 in efcb593

    /**
    * The milliseconds to debounce when a file is saved before reloading.
    * The only way to set this option is to set the `WXT_WATCH_DEBOUNCE`
    * environment variable, either globally (like in `.bashrc` file) or
    * per-project (in `.env` file).
    *
    * For example:
    * ```
    * # ~/.zshrc
    * export WXT_WATCH_DEBOUNCE=1000
    * ```
    * or
    * ```
    * # .env
    * WXT_WATCH_DEBOUNCE=1000
    * ```
    * @default 800
    */
    watchDebounce: number;

  27. aklinker1 commented on Sep 26, 2024

    @aklinker1
    Member

    Gonna go ahead and close this as completed!

  28. 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