wxt-dev / wxt

⚡ Next-gen Web Extension Framework
https://wxt.dev
MIT License
2.87k stars 88 forks source link

In Safari on iOS, the service worker seems to have been uninstalled #656

Closed asasas234 closed 1 week ago

asasas234 commented 1 week ago

After my plugin is converted to Safari through xcrun, it runs well at the beginning. After running for about 5 minutes, manually lock the screen, then open it, refresh the page, and run the plugin again. It will be found that the service_worker seems to have been uninstalled because I simply send a message to the service_worker through browser.runtime.sendMessage, expecting to get a hard-coded "hello world" string, but actually getting "undefined".

I tried configuring persistent: true, but it had no effect.

aklinker1 commented 1 week ago

Did you check to see if there are any error logs? Maybe the service worker crashed on startup.

Also, for safari, you need to add this patch to make webextension-polyfill able to send messages: https://github.com/mozilla/webextension-polyfill/issues/234#issuecomment-702403327

Safari doesn't support the promise API, so you have to polyfill it yourself. I haven't added it to WXT yet since there isn't an easy way to know if the code is running in safari... At least not that I'm aware of

asasas234 commented 1 week ago

@aklinker1 Thank you for your response, it did not crash at startup because there were no issues with the extension the first time I used it, but it continued until the device was locked, and when opened, the problem occurred

asasas234 commented 1 week ago

https://developer.apple.com/documentation/safariservices/safari_web_extensions/assessing_your_safari_web_extension_s_browser_compatibility

image

You can see that the current version of Safari already supports promises, so it doesn't seem to be this issue.

aklinker1 commented 1 week ago

Unless something changed within the last few months, that's just wrong. The message passing API didn't support promises. Though they did support promises for all the other APIs.

asasas234 commented 1 week ago

@aklinker1 I already have a solution. I only use service_worker to execute http requests. I used to include cookies in it, but now I pass them through the Header. I execute it directly on the content script. However, using browser.runtime.sendMessage does support Promise on iOS. I am sure because I have been running it successfully for a few months. The issue is that when the screen is locked, iOS unloads the service_worker, which prevents communication with it.

aklinker1 commented 1 week ago

OK, cool, good to know. Glad you figured out the reason!