chrome.runtime.onMessage sendResponse async not working

My background script is listening for chrome.runtime.onMessage and it needs to do run an async function before sending the response i.e.

chrome.runtime.onMessage.addListener(function (
  message,
  sender,
  sendResponse
) {
  setTimeout(() => {
    sendResponse("response")
  }, 100);

  return true;
})

The MDN docs for this say that returning true should allow sendResponse to happen asynchronously, and it works in our chrome extension. But in safari the content script is never getting the response.

Sending the response without async works fine.

My safari version is Version 16.1 (17614.2.9.1.13, 17614) This is a V3 manifest chrome extension that we are using xcrun safari-web-extension-converter to convert to safari

Is there a workaround for this?

Replies

According to Chrome's own documentation, "DOM-based timers, such as window.setTimeout() or window.setInterval(), are not honored in non-persistent background scripts if they trigger when the event page is dormant. Instead, use the alarms API."

https://developer.chrome.com/docs/extensions/mv2/background_migration/#timers

Ah sorry, that was just a simple example to try help reproduce, I'm not doing a timer in our actual code.

chrome.runtime.onMessage.addListener(function (
  message,
  sender,
  sendResponse
) {
  chrome.storage.local.set({"foo": 1})
    .then(() => {
      sendResponse('response');
    })

  return true;
});

has the same issue

This has always worked for me in Safari. Are you using Manifest V2 or V3?

V3

  • Hmm, I'm still using V2. I wonder if that makes a difference.

Add a Comment

Hey, are you still running into this issue? I have been running into the same bug. However, manifest v2 and v3 both do not work for me :(