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

service worker and self.addEventListener() #131

Open
bugficks opened this issue Jul 12, 2024 · 5 comments
Open

service worker and self.addEventListener() #131

bugficks opened this issue Jul 12, 2024 · 5 comments
Labels
manifest:background needs:investigation Possibly a complex ticket or a bug which fix isn't obvious

Comments

@bugficks
Copy link

I am trying to reliably detect service worker restarts. chrome.runtime.onXXX does not work if you e. g. start/stop through chrome://serviceworker-internals/.
So I've tried to add at top of my background.ts :

self.addEventListener('activate', (event) =>
{
    console.log('Service worker activated');
});

self.addEventListener('install', (event) =>
{
    console.log('Service worker installed');
});

Unfortunately this is results in:
background.ts:2 Event handler of 'activate' event must be added on the initial evaluation of worker script.
background.ts:7 Event handler of 'install' event must be added on the initial evaluation of worker script.

How can I achieve that ?

Is there any way to hook in webpack build config?

@cezaraugusto
Copy link
Member

@bugficks are you able to run the script using webpack in isolation? In the ScriptsPlugin the background scripts are added in the entry configuration, I'm not sure it's possible to run earlier than that. Here's the code that implements it. We can definitely add a patch if needed.

It's not possible to hook in the internal webpack config without a hack yet, but happy to patch a fix to make it work if that's the case

@bugficks
Copy link
Author

It seems to work using webpack in isolation. For testing I have used chrome-extension-webpack.
I could add those few lines even at end of serviceWorker.ts and had no errors in console.

image

@cezaraugusto
Copy link
Member

Possibly related to an upstream dep https://github.com/awesome-webextension/webpack-target-webextension, got to take a deeper look

@bugficks
Copy link
Author

this seems to be an issue how webpack resolves/imports modules.
In a webpack based project using my sources:
If I comment everything except those 2 addEventListener() calls in my background.ts it works with a webpack based project, if I add imports from my other ts files to background.ts I will get this error.

@cezaraugusto cezaraugusto added the needs:investigation Possibly a complex ticket or a bug which fix isn't obvious label Aug 2, 2024
@cezaraugusto
Copy link
Member

Thanks, this needs a deeper look but currently focused on the next release. Not sure how to fix this one at the moment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
manifest:background needs:investigation Possibly a complex ticket or a bug which fix isn't obvious
Projects
None yet
Development

No branches or pull requests

2 participants