v3.1.1: web workers are broken because importScripts() cannot load socket.io.js
Describe the bug
Using v3.1.1 and calling importScripts() in a worker to load socket.io.js results in a NetworkError, such as the following:
Use of importScripts() works fine with v2.
To Reproduce
The attached zip file contains 4 files: v2.html, v2.js, along with v3.html, v3.js ... but they are short:
v2.html:
<html>
<body>
<script>
let v2 = new Worker("v2.js");
</script>
</body>
</html>
v2.js:
importScripts("https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js");
v3.html:
<html>
<body>
<script>
let v3 = new Worker("v3.js");
</script>
</body>
</html>
v3.js:
importScripts("https://cdn.socket.io/3.1.1/socket.io.js");
These two test cases use CDN files for the sake of simplicity. I am not using CDN files in my project, but get the same result.
To see the correct behavior (no error), load v2.html. To see the error in the JavaScript console, load v3.html.
According to https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts, the Network error is indicates:
Expected behavior
I expect importScripts() to load socket.io.js successfully.
Platform:
- Device: Macbook Pro
- OS: 10.15.7
Thanks, I could indeed reproduce. That's weird though, the content type seems to be sent:
$ curl -I https://cdn.socket.io/3.1.1/socket.io.js
HTTP/2 200
content-type: application/javascript; charset=utf-8
content-length: 177807
content-disposition: inline; filename="socket.io.js"
access-control-allow-origin: *
etag: W/"99ea4f8096293ee4149c36c8e2251d61612a84552db8e3d1232f56a6e48efd7f"
accept-ranges: bytes
x-vercel-cache: HIT
server: Vercel
x-vercel-id: cdg1::zh794-1614003172182-e87394e0d720
strict-transport-security: max-age=63072000
date: Mon, 22 Feb 2021 14:12:59 GMT
cache-control: s-maxage=0
x-cache: RefreshHit from cloudfront
via: 1.1 9946ab784d1328de1a34d3840ab6cea6.cloudfront.net (CloudFront)
x-amz-cf-pop: CDG3-C1
x-amz-cf-id: IOqgWrelmezJBucFInf1YzeHubaUxMdbERVxOHxLMM6-jpwdNJ-Bgg==
~~@darrachequesne FWIW, and according to https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts, the MIME type should be text/javascript, not application/javascript ... but I'm not an expert in this arena ...~~ Never mind, I guess application/javascript is now the preferred content-type ... not sure what is going on ...
@darrachequesne The returned headers from v2 and v3 look more or less that same:
# v2
% curl -I https://js9.si.edu:2718/socket.io/socket.io.js
HTTP/1.1 200 OK
Content-Type: application/javascript
ETag: "2.1.1"
Date: Mon, 22 Feb 2021 22:42:22 GMT
Connection: keep-alive
# v3
% curl -I https://js9.si.edu:2718/socket.io-3/socket.io.js
HTTP/1.1 200 OK
Cache-Control: public, max-age=0
Content-Type: application/javascript
ETag: "3.0.3"
X-SourceMap: socket.io.js.map
Date: Mon, 22 Feb 2021 22:42:36 GMT
Connection: keep-alive
and (in agreement with your comment above) I believe the content is being transferred, so the problem must be in the content itself?
I think I found the culprit: https://github.com/socketio/socket.io-client/commit/8c08c5d5c3bd6dd5477ca11a2625314da57724cc
Which causes "window is not defined" when used in a Worker. It works with [email protected] (before this change).
@darrachequesne weird ... fyi, I do see a "window is not defined" error in Firefox if I load my v3.html worker test case (but not Chrome, where I generally do my debugging):

This should be fixed by https://github.com/socketio/socket.io-client/commit/13b32b39a4c1cf4829144fb0a95c4d0506000fb3, included in [email protected].
Thanks very much!