Open polisen opened 2 years ago
I just got this error when upgrading from 12.0.9 to 12.1.0.
Calling code (runs on the main thread):
import type { AnalysisWorkerInterface } from 'client/workers/analysis/main'
import { wrap } from 'comlink'
import { nanoid } from 'nanoid'
export const analysisWorker = wrap<AnalysisWorkerInterface>(
new Worker(
new URL(
/* webpackChunkName: "analysis.worker" */
'client/workers/analysis/main',
import.meta.url
),
{
// Using nanoid here to detect multiple instances running in parallel
name:
process.env.NODE_ENV === 'production'
? 'analysis'
: `analysis (${nanoid()})`
}
)
)
Can confirm that this seems to be a bug introduced since 12.1.0.
It seems I'm also affected by this issue. Is there a workaround to use Worker
in the meantime?
Chiming back after running some experiments: the issue is SSR. The Worker object is not available when rendering on the server, but works fine when running in the browser.
So this is a matter of making sure we're importing code that calls new Worker
only on the client, either:
dynamic
component import (eg: for long-lived workers that would be imported in _app.tsx
)import
, at the call site (to create a short-lived worker to offload a heavy computation)
What version of Next.js are you using?
11.1.2
What version of Node.js are you using?
14.0.0
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
other platform
Describe the Bug
I'm using next with typescript and trying to load a webworker.
For some reason - when I'm trying to initialize the webworker when I'm creating a hook - next tells me that Worker is not defined.
I'm using comlink but I suspect it's an issue with Next since comlink isn't in the mix at the point of error.
You can find the problematic line in below code snippet
useWorker.hooks.ts
in the functionmakeWorkerApiAndCleanup
const worker = new Worker(url);
useWorker.hooks.ts
useWorker.worker.ts
useWorker/index.ts
Expected Behavior
Well that the Window() constructor to be defined in any context.
To Reproduce
Here's a minimal repo:
https://github.com/polisen/browser-file-bundler