Safari Extension Service Worker Permanently Killed on iOS 17.4.x-17.6

Since probably the late iOS 17.4.x, 17.5.1 and still now in 17.6 beta our extension has been experiencing issues with the accompanying background script or service worker being permanently killed with no warning after about 30-45 seconds after initial installation (installation, not page load!).

In all other browsers (including Safari on MacOS) unloading the service worker is part of the normal lifecycle to save memory and CPU if it is idle. In our extension the service worker is used only during the first 5-10 seconds of every page visit, so we are used to seeing it unload after that and consider this a good thing. However, normally, the service worker is able to wake back up when needed - which is no longer the case in iOS.

Once dead, nothing a normal user would do can wake the service worker back up:

  • No events like webNavigation or similar will trigger anymore
  • Any attempt to call sendMessage to it from a content-script also does not wake up the service worker and instead returns undefined to the content script immediately
  • Closing and opening Safari does not start it again

The only two things that will give the service worker another 30-40 seconds of life is a reboot of the device or disabling and then re-enabling the extension. During those few second the extension is working perfectly.

There are no errors or indications in the logs of what is going on and the extension works just fine in Chrome, Firefox, Edge as well as Safari on MacOS and Safari in the Mobile simulator. Only actual iOS devices fail.

It seems like a temporary workaround is to change the manifest to not load the service worker as a service worker by changing

"background": {
	"service_worker": "service.js"
}

to

"background": {
	"scripts": ["service.js"],
	"persistent": false
}

With this change (courtesy of https://forums.developer.apple.com/forums/thread/721222) the service worker is still unloaded but correctly starts up again when needed. Having to make this change does not seem to be consistent with manifest v3 specs though (see this part in Chrome’s migration guide as an example: https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers#update-bg-field).

According to the release notes of 17.6 beta this bug was supposedly fixed: “Fixed an issue where Safari Web Extension background pages would stop responding after about 30 seconds. (127681420)”

However, this bug is not fixed - or at least not entirely fixed. It seems to work better for super simple tests doing nothing but pinging the service worker from the content script, but for the full blown extension there is no difference at all between 17.5.1 and 17.6.

Has there been a change in policy about service workers and background scripts for Safari in iOS? Are anyone else seeing this issue?

Also seemingly related:

Have you had a chance to test with the latest iOS 18 beta as well? If so, do you see issues with your extension on that build as well?

Could you file Feedback at http://feedbackassistant.apple.com/ and share that Feedback ID here? To be the most helpful, include a sample project along with a sysdiagnose captured after reproducing the issue.

As described above accurately, the issue is reproduced easily and consistently on all iOS versions after 17.4. Including iOS 18 beta and iOS 17.6 beta.

It is true that the 30 seconds limit that happens on iOS 17.4 - 17.5.1 is not happening since the "fix" on iOS 18 beta, but the service worker still stop waking up to handle messages after a very short while (more than 30 seconds, but it it doesn't take long after exiting safari or locking/unlocking phone).

Apple's repeated response of requesting feedback from developers about this issue is starting to look unprofessional as this is an issue that can clearly and easily demonstrated using the default template code for safari extension created by xcode.

The experience in iOS 18 beta 2 is different from iOS 17.6 beta and 17.5.1.

In iOS 18 the service worker is "invisible" in that it does not show up in the Safari debugger like it used to. We had a tester try it out for a little while and he did not see the service worker getting killed.

Still not fixed for iOS 18

This has been fixed in the 17.6.1 update.

@CAVI

Has this problem been resolved? I am facing the same issue, so if you have solved it, please let me know what you did. I have reported in the post below that I am facing the same issue. https://forums.developer.apple.com/forums/thread/764594

It's not fixed in 17.6.1 or 18.

Safari Extension Service Worker Permanently Killed on iOS 17.4.x-17.6
 
 
Q