How to use WebWorker with turbopack?
Answered
Virginia Rail posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Virginia RailOP
I tired to use window.Worker, but have error:
TestWorker.ts
page.tsx
error:
TestWorker.ts
onmessage = (e) => {
console.log("[Thread: 0]: " + "Test")
}
page.tsx
useEffect(() => {
let worker0 = new window.Worker(new URL('TestWorker.ts', import.meta.url));
console.log("Main Thread");
return () => worker0.terminate();
}, [])
error:
Refused to execute script from 'http://localhost:3000/_next/static/media/TestWorker.7afdbfd6.ts' because its MIME type ('video/mp2t') is not executable.
5 Replies
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
can you try
new Worker()
?Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Virginia RailOP
Yep, thanks
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
let me know if it works for you
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Virginia RailOP
I did some change:
page.tsx (Use filename without extension and put file in folder "src/workers/")
src/workers/testWorker.ts
Its working fine
page.tsx (Use filename without extension and put file in folder "src/workers/")
useEffect(() => {
let worker0 = new Worker(new URL('../workers/TestWorker', import.meta.url));
console.log("Main Thread");
worker0.postMessage("Hello World");
return () => worker0.terminate();
}, [])
src/workers/testWorker.ts
onmessage = (e) => {
console.log("[Thread: 0]: " + "Test");
console.log(e);
}
Its working fine
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
cool