Safari Web Extensions: tabs.onRemoved unexpectedly thrown on url changed.

I have noticed that tabs.onRemoved works differently in Safari 18 comparing to other browsers and Safari 17.

  • Open a tab e.g. apple.com
  • Take note of the active tab id using this code:
browser.tabs.query({currentWindow: true, active: true}, (x) => console.log(x[0].id)) 
  • Add a listener on onRemoved:
browser.tabs.onRemoved.addListener(console.log)
  • on the active tab, navigate to e.g. https://developer.mozilla.org
  • Take note of the active tab id again (using the same tabs.query).

Expect:

  • there should be no console.log of onRemoved.
  • the active tab id stays the same.

Actual:

  • there is a console.log of onRemoved.
  • the active tab id is changed.

Please help. If this is a bug introduced in Safari 18, it would break a lot of JS Web extensions.

This is expected behavior. See: https://forums.developer.apple.com/forums/thread/763250?answerId=802635022#802635022

You will want to listen for tabs.onReplaced as well.

tabs.onReplaced can be fired in Safari 18 during actions like redirecting to an internal extension page or preloading a Top Hit. This event can also occur in Chrome under certain conditions. It’s important to handle this event and update any internal tab ID state mappings in your extension to ensure consistent behavior across all types of tab navigations.

Safari Web Extensions: tabs.onRemoved unexpectedly thrown on url changed.
 
 
Q