developit / web-worker

Consistent Web Workers in browser and Node.
https://npm.im/web-worker
Apache License 2.0
1.06k stars 55 forks source link

Relative URL handling #3

Closed guybedford closed 4 years ago

guybedford commented 4 years ago

I see you are using the stack trace hack currently to determine the relative URL.

The issue with this approach is that it doesn't actually match what the browser does which is to resolve new Worker('./rel') relative to the page, not the URL of the module that does the worker instantiation.

As a result, the best approach for a cross-browser worker instantiation is actually something like:

new Worker(new URL('./rel.js', import.meta.url))

I would really like to see us encouraging the above pattern as something tools and environments can get behind. Build tools can easily then parse and optimize this pattern with a simple detection.

This would involve the following changes:

  1. Allowing a URL object into new Worker (does the browser call toString? I think it might?)
  2. Changing the readme to use this pattern in the example
  3. Removing the stack hack

Let me know if you might consider the above, I'd be glad to contribute too.

developit commented 4 years ago

Good point, definitely would prefer to push folks to use that pattern.

I can throw the stack hack thing into another package - I've found some cases where doing it without that gets really ugly, but it's the wrong thing to push folks towards for sure.

I was thinking of Thursday for a release date - think you'd have a chance to throw a PR together be then? If not I can make the changes and run them by you.

guybedford commented 4 years ago

Thanks for being open to it! I've created a PR at https://github.com/developit/web-worker/pull/5.