studio-lagier / next-wasm-workers

Example repo for getting NextJS, Rust via wasm-pack, and web workers all playing nicely together.
73 stars 13 forks source link

Can't find worker file #1

Closed niklasravnsborg closed 3 years ago

niklasravnsborg commented 3 years ago

Hey @tomlagier!

Thanks for your article and this repo. I tried to use it as a reference for a project where I try to load a web worker that is written in Typescript with Next 11 / Webpack 5. In my project I couldn't get Next.js to expose the worker file so I tried to download your repository. It turns out, I get the same error here:

Screenshot 2021-07-28 at 13 13 40

I just added a console.log statement to index.tsx.

const workerUrl = new URL('../src/ts.worker.ts', import.meta.url);
console.log(workerUrl);
tsWorkerRef.current = new Worker(workerUrl);

When I manually navigate to http://localhost:3000/_next/94ab9802796300a474cd.ts, I also get the 404. I would assume, that you should get the same behavior. Do you have an idea on how to fix this?

tomlagier commented 3 years ago

Hmm.. I'll take a look now.

tomlagier commented 3 years ago

Interestingly, I'm not getting this behavior with either next dev or next build && next start. If you next build, what's the .next folder look like? Can you find the missing file there?

image

Here's the network requests after next build && next start: image

I think it's gotta be an environment issue here - something bound to the port, some version mismatch, etc.

niklasravnsborg commented 3 years ago

Hey, it turns out, adding the workerUrl constant is causing the issue:

This works fine:

tsWorkerRef.current = new Worker(new URL('../src/ts.worker.ts', import.meta.url));

This causes the error I experienced:

const workerUrl = new URL('../src/ts.worker.ts', import.meta.url);
console.log(workerUrl);
tsWorkerRef.current = new Worker(workerUrl);

I guess this is some kind of Webpack behavior and it can only bundle static webworker files when they can be analyzed in one statement.

Thank you for checking :)