Skip to content
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

Closed
aklinker1 opened this issue Jul 20, 2023 · 6 comments
Closed

Comments

@aklinker1
Copy link
Collaborator

aklinker1 commented Jul 20, 2023

From Chrome's debugging article: https://developer.chrome.com/docs/extensions/mv3/tut_debugging/

Also, if you have made changes to the service worker code, you can use the Update button and skipWaiting to apply the changes immediately.

screesnhot

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?

@aklinker1
Copy link
Collaborator Author

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();
  };
});
Screenshot 2023-07-20 at 4 01 40 PM

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:

Screenshot 2023-07-20 at 4 15 11 PM

@aklinker1
Copy link
Collaborator Author

Here's a minimal extension demoing this behavior: minimal-example.zip

@aklinker1
Copy link
Collaborator Author

Conversation on the chrome google groups: https://groups.google.com/a/chromium.org/g/chromium-extensions/c/Z-H3IkRODpQ

@aklinker1 aklinker1 added this to the v0.4 milestone Jul 21, 2023
@aklinker1
Copy link
Collaborator Author

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

@aklinker1
Copy link
Collaborator Author

And nevermind, will break in Chrome 117...

https://chromiumdash.appspot.com/commit/b8b06b6b8071b96e3f76858d662d8991ba3b3374

@aklinker1 aklinker1 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2023
@aklinker1
Copy link
Collaborator Author

Can confirm that you can no longer update the service worker to get the latest code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant