Code Monkey home page Code Monkey logo

Comments (6)

aklinker1 avatar aklinker1 commented on May 20, 2024

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

from wxt.

aklinker1 avatar aklinker1 commented on May 20, 2024

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

from wxt.

aklinker1 avatar aklinker1 commented on May 20, 2024

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

from wxt.

aklinker1 avatar aklinker1 commented on May 20, 2024

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.

aklinker1 avatar aklinker1 commented on May 20, 2024

And nevermind, will break in Chrome 117...

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

from wxt.

aklinker1 avatar aklinker1 commented on May 20, 2024

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

from wxt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.