Next.js Discord

Discord Forum

WebWorker in AppRouter - document is not defined

Answered
Luke posted this in #help-forum
Open in Discord
workerRef.current = new Worker(new URL("../worker.ts", import.meta.url)); this throws an error document is not defined even inside a useEffect

It appears a worker path cannot be dynamic at all. Is there a way to create a static path to a webworker .ts file that will work in dev and prod?
Answered by Luke
Resolved 👀

I believed this was an issue related to app vs page router, but the [page router example of using a web-worker](https://github.com/vercel/next.js/tree/canary/examples/with-web-worker) works just fine. Until, your page is built with an edge runtime.

Disabling the following:

export const runtime = "edge";


This now works just fine.
View full answer

2 Replies

Resolved 👀

I believed this was an issue related to app vs page router, but the [page router example of using a web-worker](https://github.com/vercel/next.js/tree/canary/examples/with-web-worker) works just fine. Until, your page is built with an edge runtime.

Disabling the following:

export const runtime = "edge";


This now works just fine.
Answer