crxjs / chrome-extension-tools

Bundling Chrome Extensions can be pretty complex. It doesn't have to be.
https://crxjs.dev/vite-plugin
2.75k stars 182 forks source link

"Extension context invalidated" errors #673

Open thdoan opened 1 year ago

thdoan commented 1 year ago

Build tool

Rollup

Where do you see the problem?

Describe the bug

When running npm run dev (alias to vite), I often get "Extension context invalidated" errors for all the open tabs. Example:

image

If this cannot be avoided, is there a recommended way to prevent this, or maybe you can suppress this in the code?

Reproduction

  1. Create a vite + crxjs extension with content script locally.
  2. Run npm run dev or vite.
  3. Stop the server and re-run step 2.
  4. Repeat steps 2-3 enough times and you'll start seeing errors similar to the screenshot above.

Logs

N/A

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 15.23 GB / 31.95 GB
  Binaries:
    Node: 16.19.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.19.3 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (111.0.1661.44)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @crxjs/vite-plugin: ^2.0.0-beta.15 => 2.0.0-beta.15
    vite: ^4.2.1 => 4.2.1

Severity

annoyance

sublimator commented 1 year ago

Though I haven't looked in detail at your report, I can say that it's normal during extension dev to see a lot of those. When the extension fully reloads all the content scripts running the old version are invalid.

thdoan commented 1 year ago

From the error log, it seems this error is related to stale ports -- would it be possible to use the port's disconnect handler to catch when the extension is unloaded to prevent "Extension context invalidated" errors? Or maybe just suppress the errors altogether. It would be nice not to see the error logs flooded with these errors because they are act as false positives, meaning every time these errors show up, I have to manually go and read the stack trace because I don't know if the error is something in my code, or something that can be safely ignored like "Extension context invalidated" errors.

ongnxco commented 1 year ago

From the error log, it seems this error is related to stale ports -- would it be possible to use the port's disconnect handler to catch when the extension is unloaded to prevent "Extension context invalidated" errors? Or maybe just suppress the errors altogether. It would be nice not to see the error logs flooded with these errors because they are act as false positives, meaning every time these errors show up, I have to manually go and read the stack trace because I don't know if the error is something in my code, or something that can be safely ignored like "Extension context invalidated" errors.

Giải quyết được lỗi này chưa bạn?