Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Add a worker implementation based on fetch #225

Closed
otakustay opened this issue Dec 12, 2019 · 7 comments
Closed

Add a worker implementation based on fetch #225

otakustay opened this issue Dec 12, 2019 · 7 comments

Comments

@otakustay
Copy link

We were using worker-loader and encountered CORS issue, the inline option works but make bundle much larger.

I'm wondering if we can provide a FetchWorker like:

const fetchWorker = url => {
    const pendingMessages = [];
    const listeners = [];
    let worker = null;

    const newWorker = text => {
        worker = inlineWorker(text, url);
        for (const [message, transferList] of pendingMessages) {
            worker.postMessage(message, transferList);
        }
        pendingMessages.length = 0;
    };

    fetch(url).then(r => r.text()).then(newWorker);

    return {
        postMessage(message, transferList) {
            if (worker) {
                worker.postMessage(message, transferList);
            }
            else {
                pendingMessages.push([message, transferList]);
            }
        },

        addEventListener() {
            // ...
        }
    }
}

With this implementation we can move worker to a separated file just like inline is disabled, but read it via fetch to come across CORS issue.

@otakustay
Copy link
Author

Or this can be implemented via importScripts function

@alexander-akait
Copy link
Member

#255, you can set credentials vie options

@otakustay
Copy link
Author

It seems credentials option does not address cors issues, worker doesn't support cors script no matter what credentials is.

@alexander-akait
Copy link
Member

I can't understand the problem

We were using worker-loader and encountered CORS issue

What?

I'm wondering if we can provide a FetchWorker like:

It is out of scope web worker supporting

@otakustay
Copy link
Author

When publicPath is set to an external domain like http://cdn.my-app.com/assets/, worker-loader generated worker script can fail to load, just as documented: https://github.com/webpack-contrib/worker-loader#cross-origin-policy

To address this, document suggests inline option but this increase bundle size impressively

There is a way to get rid of the CORS issue and having worker scripts in a separated bundle if worker-loader can generate its content to a custom blob and a importScripts call according to this article: https://benohead.com/blog/2017/12/06/cross-domain-cross-browser-web-workers/

@alexander-akait
Copy link
Member

I think we can do it, do you want to send a PR?

@alexander-akait
Copy link
Member

Let's close in favor #281, I think solution here https://benohead.com/blog/2017/12/06/cross-domain-cross-browser-web-workers/ is good

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

No branches or pull requests

2 participants