-
Notifications
You must be signed in to change notification settings - Fork 103
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
Reload background service worker without reloading entire extension #53
Comments
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 Unfortunately, calling the |
Here's a minimal extension demoing this behavior: minimal-example.zip |
Conversation on the chrome google groups: https://groups.google.com/a/chromium.org/g/chromium-extensions/c/Z-H3IkRODpQ |
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 |
And nevermind, will break in Chrome 117... https://chromiumdash.appspot.com/commit/b8b06b6b8071b96e3f76858d662d8991ba3b3374 |
Can confirm that you can no longer update the service worker to get the latest code. |
From Chrome's debugging article: https://developer.chrome.com/docs/extensions/mv3/tut_debugging/
If there's a way to do this programatically, that would be great. It means extension pages wouldn't have to be closed when changing the background.
Maybe we have access to these ServiceWorker APIs inside the background?
The text was updated successfully, but these errors were encountered: