Pretty much what the title says. What is weirder is that this only happens when you run the program in a new tab, if you merely refresh the page, there will be only one message for each console.log
.
Here is main.js
:
const worker = new Worker('worker.js');
Here is worker.js
:
console.log('Foo bar!');
Here is index.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Web Workers</title>
<style></style>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
The same is not happening in Firefox. I am using Chrome version 65, what's more odd is that the duplicate message seems to belong to no contexts listed in the "contexts" dropdown, so the only message listed when I try to filter out all console messages aside from ones sent by worker.js
is the first message, the second message (the duplicate) seems to belong to no contexts.
@bean's answer revealed that there is already a question like this asked (from February 2018, I believe) which has not been answered. If no one answers this question either, I will consider raising an issue in Chrome's development forum/whatever.
Pretty much what the title says. What is weirder is that this only happens when you run the program in a new tab, if you merely refresh the page, there will be only one message for each console.log
.
Here is main.js
:
const worker = new Worker('worker.js');
Here is worker.js
:
console.log('Foo bar!');
Here is index.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Web Workers</title>
<style></style>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
The same is not happening in Firefox. I am using Chrome version 65, what's more odd is that the duplicate message seems to belong to no contexts listed in the "contexts" dropdown, so the only message listed when I try to filter out all console messages aside from ones sent by worker.js
is the first message, the second message (the duplicate) seems to belong to no contexts.
@bean's answer revealed that there is already a question like this asked (from February 2018, I believe) which has not been answered. If no one answers this question either, I will consider raising an issue in Chrome's development forum/whatever.
Share Improve this question edited Apr 8, 2018 at 10:49 doubleOrt asked Apr 4, 2018 at 20:02 doubleOrtdoubleOrt 2,5071 gold badge16 silver badges36 bronze badges 2- @TarunLalwani localhost, I believe loading files locally does not work in Chrome (for Workers). – doubleOrt Commented Apr 8, 2018 at 9:31
-
@TarunLalwani local Workers don't work in Chrome, which is why I am accessing the file from
localhost
. If I am not mixing this up withServiceWorkers
, Workers only work in HTTPS andlocalhost
. – doubleOrt Commented Apr 8, 2018 at 9:38
1 Answer
Reset to default 7 +25This seems to be a strange behavior in Chrome, and someone has posted the same issue here Console.log double logging from web workers.
However, placing console.log
directly inside worker.js
is not a good practice. A web worker runs in a separate thread and listens to the messages posted from main.js
and take actions accordingly, thus any business logic should be placed inside the onmessage
event handler. By doing this, you don't have to worry about code being executed twice.