How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue?

Laravelvue.jsGoogle ChromeGoogle Experiments

Laravel Problem Overview


I'm using VueJS and Laravel for my project. This issue started to show lately and it shows even in the old git branches.

This error only shows in the Chrome browser.

Laravel Solutions


Solution 1 - Laravel

I disabled all installed extensions in Chrome - works for me. I have now clear console without errors.

Solution 2 - Laravel

In case you're an extension developer who googled your way here trying to stop causing this error:

The issue isn't CORB (as another answer here states) as blocked CORs manifest as warnings like -

> Cross-Origin Read Blocking (CORB) blocked cross-origin response > https://www.example.com/example.html with MIME type text/html. See > https://www.chromestatus.com/feature/5629709824032768 for more > details.

The issue is most likely a mishandled async response to runtime.sendMessage. As MDN says:

> To send an asynchronous response, there are two options: > > * return true from the event listener. This keeps the sendResponse > function valid after the listener returns, so you can call it later. > * return a Promise from the event listener, and resolve > when you have the response (or reject it in case of an error).

When you send an async response but fail to use either of these mechanisms, the supplied sendResponse argument to sendMessage goes out of scope and the result is exactly as the error message says: your message port (the message-passing apparatus) is closed before the response was received.

Webextension-polyfill authors have already written about it in June 2018.

So bottom line, if you see your extension causing these errors - inspect closely all your onMessage listeners. Some of them probably need to start returning promises (marking them as async should be enough). [Thanks @vdegenne]

Solution 3 - Laravel

If you go to chrome://extensions/, you can just toggle each extension one at a time and see which one is actually triggering the issue.

Once you toggle the extension off, refresh the page where you are seeing the error and wiggle the mouse around, or click. Mouse actions are the things that are throwing errors.

So I was able to pinpoint which extension was actually causing the issue and disable it.

Solution 4 - Laravel

Post is rather old and not closely related to Chrome extensions development, but let it be here.

I had same problem when responding on message in callback. The solution is to return true in background message listener.

Here is simple example of background.js. It responses to any message from popup.js.

chrome.runtime.onMessage.addListener(function(rq, sender, sendResponse) {
    // setTimeout to simulate any callback (even from storage.sync)
    setTimeout(function() {
        sendResponse({status: true});
    }, 1);
    // return true;  // uncomment this line to fix error
});

Here is popup.js, which sends message on popup. You'll get exceptions until you un-comment "return true" line in background.js file.

document.addEventListener("DOMContentLoaded", () => {
    chrome.extension.sendMessage({action: "ping"}, function(resp) {
        console.log(JSON.stringify(resp));
    });
});

manifest.json, just in case :) Pay attention on alarm permissions section!

{
  "name": "TestMessages",
  "version": "0.1.0",
  "manifest_version": 2,
  "browser_action": {
    "default_popup": "src/popup.html"
  },
  "background": {
    "scripts": ["src/background.js"],
    "persistent": false
  },
  "permissions": [
    "alarms"
  ]
}

Solution 5 - Laravel

This error is generally caused by one of your Chrome extensions.

I recommend installing this One-Click Extension Disabler, I use it with the keyboard shortcut COMMAND (⌘) + SHIFT (⇧) + D — to quickly disable/enable all my extensions.

Once the extensions are disabled this error message should go away.

Peace! ✌️

Solution 6 - Laravel

If error reason is extension use incognito Ctrl+Shift+N. In incognito mode Chrome does not have extensions.

UPD. If you need some extension in incognito mode e.g. ReduxDevTools or any other, in extension settings turn on "Allow in incognito"

Solution 7 - Laravel

Make sure you are using the correct syntax.

We should use the sendMessage() method after listening it.

Here is a simple example of contentScript.js It sendRequest to app.js.

contentScript.js

chrome.extension.sendRequest({
    title: 'giveSomeTitle', params: paramsToSend
  }, function(result) { 
    // Do Some action
});

app.js

chrome.extension.onRequest.addListener( function(message, sender, 
 sendResponse) {
  if(message.title === 'giveSomeTitle'){
    // Do some action with message.params
    sendResponse(true);
  }
});

Solution 8 - Laravel

For those coming here to debug this error in Chrome 73, one possibility is because Chrome 73 onwards disallows cross-origin requests in content scripts.

More reading:

  1. https://www.chromestatus.com/feature/5629709824032768
  2. https://www.chromium.org/Home/chromium-security/extension-content-script-fetches

This affects many Chrome extension authors, who now need to scramble to fix the extensions because Chrome thinks "Our data shows that most extensions will not be affected by this change."

(it has nothing to do with your app code)

UPDATE: I fixed the CORs issue but I still see this error. I suspect it is Chrome's fault here.

Solution 9 - Laravel

In my case it was a breakpoint set in my own page source. If I removed or disabled the breakpoint then the error would clear up.

The breakpoint was in a moderately complex chunk of rendering code. Other breakpoints in different parts of the page had no such effect. I was not able to work out a simple test case that always trigger this error.

Solution 10 - Laravel

I suggest you first disable all the extensions then one by one enable them until you find the one that has caused the issue in my case Natural Reader Text to Speech was causing this error so I disabled it. nothing to do with Cross-Origin Read Blocking (CORB) unless the error mention Cross-Origin then further up the tread it is worthwhile trying that approach.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTriumf MaqedonciView Question on Stackoverflow
Solution 1 - LaravelMirekHView Answer on Stackoverflow
Solution 2 - LaravelOfek ShilonView Answer on Stackoverflow
Solution 3 - LaravelAguaymaView Answer on Stackoverflow
Solution 4 - LaravelAleksej VasinovView Answer on Stackoverflow
Solution 5 - LaravelAhmad AwaisView Answer on Stackoverflow
Solution 6 - LaravelairushView Answer on Stackoverflow
Solution 7 - LaravelGopal B ShimpiView Answer on Stackoverflow
Solution 8 - LaravelJonathan LinView Answer on Stackoverflow
Solution 9 - LaravelAnthonyVOView Answer on Stackoverflow
Solution 10 - LaravelWaheed RafiqView Answer on Stackoverflow