Search code examples
javascriptweb-workerfetch-api

How to test if Fetch API is available in Web Worker from main frame


I'm using the Fetch API when available in a browser by simply testing the window.fetch.

However in the case of Safari 10, fetch is available in the main frame, which instantiate a web worker but it is not available in the web worker frame.

Is there a way to test if a top level function is available in a web worker from the main frame ?

Ideally, we could have a reference of the web worker instance and test if a property is available on the self object.


Solution

  • Is there a way to test if a top level function is available in a web worker from the main frame ?

    Not synchronously.

    However, in many browsers you can create a Web Worker instance from a blob or data-uri. You could create a web worker that runs a typeof test and sends the result back as a message. See How to create a Web Worker from a string for more information.

    I'd suggest that this may be one of those rare instances where browser version detection is more appropriate.