Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.1.1: web workers are broken because importScripts() cannot load socket.io.js #3809

Closed
ericmandel opened this issue Feb 19, 2021 · 7 comments
Labels
bug Something isn't working
Milestone

Comments

@ericmandel
Copy link

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:

Screen Shot 2021-02-18 at 6 48 15 PM

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:

Screen Shot 2021-02-18 at 6 54 53 PM

Expected behavior

I expect importScripts() to load socket.io.js successfully.

Platform:

  • Device: Macbook Pro
  • OS: 10.15.7

socketioImportScripts.zip

@ericmandel ericmandel added the bug Something isn't working label Feb 19, 2021
@ericmandel ericmandel changed the title importScripts() fails to load socket.io.js in v3.1.1 v3.1.1: web workers are broken because importScripts() cannot load socket.io.js Feb 20, 2021
@darrachequesne
Copy link
Member

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==

@ericmandel
Copy link
Author

ericmandel commented Feb 22, 2021

@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 ...

@ericmandel
Copy link
Author

@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?

@darrachequesne
Copy link
Member

I think I found the culprit: socketio/socket.io-client@8c08c5d

Which causes "window is not defined" when used in a Worker. It works with socket.io-client@3.0.4 (before this change).

@ericmandel
Copy link
Author

@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):

Screen Shot 2021-02-23 at 7 10 01 AM

darrachequesne added a commit to socketio/socket.io-client that referenced this issue Feb 26, 2021
A previous change ([1], included in 3.0.5) broke support for web
workers, which threw "window is not defined" when importing the bundle
in a web worker.

Related: socketio/socket.io#3809

Reference: https://webpack.js.org/configuration/output/#outputglobalobject

[1]: 8c08c5d
@darrachequesne
Copy link
Member

This should be fixed by socketio/socket.io-client@13b32b3, included in socket.io-client@3.1.2.

@darrachequesne darrachequesne added this to the 3.1.2 milestone Feb 26, 2021
@ericmandel
Copy link
Author

Thanks very much!

sunrise30 added a commit to sunrise30/socket.io-client that referenced this issue Jan 8, 2022
A previous change ([1], included in 3.0.5) broke support for web
workers, which threw "window is not defined" when importing the bundle
in a web worker.

Related: socketio/socket.io#3809

Reference: https://webpack.js.org/configuration/output/#outputglobalobject

[1]: socketio/socket.io-client@8c08c5d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants