Skip to content

Error: Could not establish connection. Receiving end does not exist. after redirect? #670

Answered by aklinker1
Wizzel1 asked this question in Q&A
Discussion options

You must be logged in to vote

I don't know if it's the main cause, but one problem is in your use of sendResponse.

To respond to a message asynchronously, you should either:

  • Return true synchronously and call sendResponse at some point in the future
  • OR return a promise of the response.

You cannot do both. These three snippets behave the same:

  1. Synchronously return true and use sendResponse

    // background.ts
    browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
      doSomething().then(() => sendResponse(123));
      return true;
    });
  2. Don't use an async function, but return a promise

    // background.ts
    browser.runtime.onMessage.addListener((message, sender, _) => {
      return doSomething().then(() => 123);
    });

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@aklinker1
Comment options

@aklinker1
Comment options

@Wizzel1
Comment options

@aklinker1
Comment options

@Wizzel1
Comment options

Answer selected by Wizzel1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants