Skip to content

Commit 012bd7e

Browse files
committed
docs: Update context usage
1 parent 35778f7 commit 012bd7e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

docs/entrypoints/content-scripts.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ When defining multiple content scripts, content script entrypoints that have the
5252

5353
## Context
5454

55-
Old content scripts are not automatically stopped when an extension updates and reloads. Often, this leads to "Invalidated context" errors in production when a content script from an old version of your extension tries to use a extension API.
55+
Old content scripts are not automatically stopped when an extension updates and reloads. Often, this leads to "Invalidated context" errors in production when a content script from an old version of your extension tries to use a web extension API (ie, the `browser` or `chrome` globals).
5656

57-
WXT provides a utility for managing this process: `ContentScriptContext`. An instance of this class is provided to you automatically inside the `main` function of your content script.
57+
WXT provides a utility for handling this process: `ContentScriptContext`. An instance of this class is provided to you automatically inside the `main` function of your content script.
58+
59+
When your extension updates or is uninstalled, the context will become invalidated, and will trigger any `ctx.onInvalidated` listeners you add:
5860

5961
```ts
6062
export default defineContentScript({
@@ -65,26 +67,33 @@ export default defineContentScript({
6567
// ...
6668
});
6769

68-
// Stop fetch requests
69-
fetch('...url', { signal: ctx.signal });
70-
71-
// Timeout utilities
70+
// Timeout utilities that are automatically cleared when invalidated
7271
ctx.setTimeout(() => {
7372
// ...
7473
}, 5e3);
7574
ctx.setInterval(() => {
7675
// ...
7776
}, 60e3);
77+
78+
// Or add event listeners that get removed when invalidated
79+
ctx.addEventListener(document, 'visibilitychange', (event) => {
80+
// ...
81+
});
82+
83+
// You can also stop fetch requests
84+
fetch('...url', { signal: ctx.signal });
7885
},
7986
});
8087
```
8188

8289
The class extends [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) and provides other utilities for stopping a content script's logic once it becomes invalidated.
8390

84-
:::tip
91+
:::warning
8592
When working with content scripts, **you should always use the `ctx` object to stop any async or future work.**
8693

8794
This prevents old content scripts from interfering with new content scripts, and prevents error messages from the console in production.
95+
96+
If you're using a framework like React, Vue, Svelte, etc., make sure you're unmounting your UI properly in the `onRemove` option of [`createShadowRootUi`](https://wxt.dev/guide/content-script-ui.html#shadow-root).
8897
:::
8998

9099
## CSS

0 commit comments

Comments
 (0)