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

Subworkers loaded from incorrect URL? #31

Open
jamiewinder opened this issue Jul 5, 2019 · 3 comments
Open

Subworkers loaded from incorrect URL? #31

jamiewinder opened this issue Jul 5, 2019 · 3 comments
Labels
question Further information is requested

Comments

@jamiewinder
Copy link

jamiewinder commented Jul 5, 2019

I have worker that is loaded from the main code:

const worker = new Worker('./MainWorker.js', {
    name: 'main',
    type: 'module'
});

And within this worker, it tries to load a subworker:

const subworker = new Worker('./Subworker.js', {
    name: 'sub',
    type: 'module'
});

When webpack bundles this, the browser correctly and successfully loads the main worker from 'dist/main.worker.js', however it then tries to load the subworker from 'dist/dist/sub.worker.js' which isn't where the worker bundle was built to.

I'm not sure if this is a config problem on my side or an issue with the plugin. Here are the relevant parts of my webpack config:

{
    entry: ['./src/index.js'],
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].js',
        publicPath: 'dist/'
    },
    plugins: [
        new WorkerPlugin({
            filename: '[name].worker.js'
        })
    ]
}
@developit
Copy link
Collaborator

developit commented Jul 16, 2019

Hi there! You need to set your output.publicPath to an absolute URL:

{
    entry: ['./src/index.js'],
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].js',
-        publicPath: 'dist/'
+        publicPath: '/dist/'
    },
    plugins: [
        new WorkerPlugin({
            filename: '[name].worker.js'
        })
    ]
}

It might be possible to address this in worker-plugin, but I'm not sure.

@jamiewinder
Copy link
Author

Thanks. I had tried that (successfully) but I want the URLs to be relative to my page rather than the root. I can't assume which URL the app will be loaded from.

@developit
Copy link
Collaborator

developit commented Jul 26, 2019

@jamiewinder I'm not sure if it will work in the worker context, but you could set the public path dynamically at runtime (more info here):

__webpack_public_path__ = location.pathname + '/dist';

@developit developit added the question Further information is requested label Mar 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants