Closing options page kills background worker

I have a pretty strange issue here. I'm developing a new Safari-only web extension, and here is the setup and the problem I am having:

  1. My extension's main UI is in the when you click on the toolbar icon.
  2. The popup uses sendMessage to communicate with the background script. When the popup opens, it loads data from the background script that way, and renders it in the popup.
  3. Additionally, I have a button in the popup that says "Advanced", and what is does is open the "options" page, for additional advanced options.

The problem I am having is that, after closing the Options page and going back to the page that you were on when the "Advanced" button was clicked, if you open the extension popup again, it doesn't load any data from the background script, as if the background script has been killed. The UI opens, but it's not populated with any data.

This only seems to happen on iOS, too. On Safari on the macOS, it works as expected. On iOS, if I refresh the browser page, or go to a new tab, the UI works correctly again.

What is it about iOS safari that is causing this, and can I mitigate it somehow?

Answered by ursuscamp in 743188022

I have figured it out. So, there were some branches from my onMessage handler in the background script that weren't returning anything, which was causing the handler to not return correctly and hang. Adding a return false for those branches resolved the issue.

Once of the reasons this was so difficult to figure out is because it was only happening on iOS, and seeing the background script logs in iOS is very difficult, and also the popup doesn't record console.log calls in the web inspector, for whatever reason. So here is how I figured it out:

  1. I changes the background in the manifest from a service_worker script to a page. Then I just created a generic HTML page with a script tag, which loaded the background script. This helped because the HTML page made the background page show up in the Develop menu in Safari for the simulator. If I just used a script, the Safari-generated background page never appeared.
  2. I created a bglog function in my popup, which allowed me to log from the popup to the background page, that way I could see logs adequately from the popup.
Accepted Answer

I have figured it out. So, there were some branches from my onMessage handler in the background script that weren't returning anything, which was causing the handler to not return correctly and hang. Adding a return false for those branches resolved the issue.

Once of the reasons this was so difficult to figure out is because it was only happening on iOS, and seeing the background script logs in iOS is very difficult, and also the popup doesn't record console.log calls in the web inspector, for whatever reason. So here is how I figured it out:

  1. I changes the background in the manifest from a service_worker script to a page. Then I just created a generic HTML page with a script tag, which loaded the background script. This helped because the HTML page made the background page show up in the Develop menu in Safari for the simulator. If I just used a script, the Safari-generated background page never appeared.
  2. I created a bglog function in my popup, which allowed me to log from the popup to the background page, that way I could see logs adequately from the popup.
Closing options page kills background worker
 
 
Q