Comments (6)
By running the following code in the top-level scope of the background service worker, I was able to access the two functions necessary to update the service worker without reloading the entire extension:
addEventListener('install', (event) => {
const sw = event.target as ServiceWorkerGlobalScope;
const { registration, skipWaiting } = sw;
const update = registration.update;
console.log({ skipWaiting, update });
globalThis.reloadBackground = async () => {
await update();
await skipWaiting();
};
});
Extension service workers can listen to the standard web worker install
event, from which we're able to access the ServiceWorkerGlobalScope
and ServiceWorkerRegistration
.
Unfortunately, calling the reloadBackground
function from the dev console results in an error:
from wxt.
Here's a minimal extension demoing this behavior: minimal-example.zip
from wxt.
Conversation on the chrome google groups: https://groups.google.com/a/chromium.org/g/chromium-extensions/c/Z-H3IkRODpQ
from wxt.
Alright, working example:
// demo/src/entrypoints/background.ts
export default defineBackground(() => {
// ...
const serviceWorker = globalThis as unknown as ServiceWorkerGlobalScope;
globalThis.addEventListener('install', () => {
void serviceWorker.skipWaiting();
});
// @ts-expect-error
globalThis.updateSelf = async () => {
await serviceWorker.registration.update();
};
});
Then you can call updateSelf
from the service worker's dev console after making a change to demo/.output/chrome-mv3/background.js
from wxt.
And nevermind, will break in Chrome 117...
https://chromiumdash.appspot.com/commit/b8b06b6b8071b96e3f76858d662d8991ba3b3374
from wxt.
Can confirm that you can no longer update the service worker to get the latest code.
from wxt.
Related Issues (20)
- Dotfiles cannot be included in the sources for Firefox
- In serve mode, when changing the App. vue file, the render function is missing
- Vitest cannot import Vue files HOT 4
- Missing Permissions Warning HOT 1
- Unable to clear `default_popup` HOT 4
- Cannot use browser when executeScript HOT 2
- Firebase Remotely hosted Code error from Chrome Developer Store
- Return values from unlisted scripts HOT 1
- chrome plugin uninstall listening HOT 1
- Type-safe for storage API like `defineExtensionMessaging` HOT 2
- wxt is required to support Chrome onInstall and onUnInstall HOT 1
- Firefox Android Dev Mode Support
- Errors Thrown When Two Nested Modules Import the Same Module HOT 2
- `import.meta.glob` doesn't work HOT 2
- Built-in Messaging API HOT 2
- Unable to retrieve defaultValue set with defineItem using getItem HOT 3
- Add dev CSP to sandbox
- In Safari on iOS, the service worker seems to have been uninstalled HOT 6
- Configure browser binaries globally on windows
- declarativeContent doesn't have a Typescript type defined
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wxt.