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

Allow URL objects when starting workers #8698

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mischnic
Copy link
Contributor

@mischnic mischnic commented Jul 1, 2021

Passing URL object into worker construtors/navigator.serviceWorker.register should be allowed

For example described here: https://webpack.js.org/guides/web-workers/

@mvitousek
Copy link
Contributor

The documentation you linked to looks good for Worker constructors; is there documentation for SharedWorker and register?

@mischnic
Copy link
Contributor Author

mischnic commented Jul 2, 2021

It's hard to find examples for this. The reason why passing an URL object works is that it is stringified and url.toString() just returns the "real" url. (Tthe benefit of the URL/import.meta.url thing is that the worker script is now actually relative to the JS file that creates the worker, just like an import - otherwise, it's relative to the current document. And since this URL->string conversion happens automatically, you can omit toString() in browsers.)

Webpack example for SharedWorker: https://github.com/webpack/webpack/blob/c181294865dca01b28e6e316636fef5f2aad4eb6/examples/worker/example.js#L25-L31

The arguments for Worker, SharedWorker, register are all USVString according to the spec, so URLs aren't specifically handled there (and all three should behave the same).

https://w3c.github.io/ServiceWorker/#idl-index
https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
https://html.spec.whatwg.org/multipage/workers.html#shared-workers-and-the-sharedworker-interface

@isker
Copy link
Contributor

isker commented Jul 9, 2021

@mischnic
Copy link
Contributor Author

mischnic commented Aug 15, 2021

@mvitousek What do you think?

As far as I can tell, Flow already does what I'm proposing here for fetch: RequestInfo is the "url or request" input type for fetch:

flow/lib/bom.js

Line 1511 in f9afdbc

type RequestInfo = Request | URL | string;

flow/lib/bom.js

Line 1601 in f9afdbc

declare function fetch(input: RequestInfo, init?: RequestOptions): Promise<Response>;

But the spec actually doesn't say that it can be a URL object (meaning Flow models this very common implicit URL to string conversion as done by URL#toString): https://fetch.spec.whatwg.org/#requestinfo

typedef (Request or USVString) RequestInfo;
interface Request {
  constructor(RequestInfo input, optional RequestInit init = {});
  ...
}

...

fetch(RequestInfo input, optional RequestInit init = {});

@mischnic
Copy link
Contributor Author

mischnic commented Oct 14, 2021

Any news regarding this ?
cc @mroch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants