vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.77k stars 26.95k forks source link

Web Worker creation heuristics #61787

Open brandonjpierce opened 9 months ago

brandonjpierce commented 9 months ago

Link to the code that reproduces this issue

https://github.com/brandonjpierce/nextjs-workers

To Reproduce

npm run dev to boot up the app. You should see a runtime exception. If you go to src/playground/worker-struct.ts you can comment out c and e in the workers object to see it "working" without the runtime exceptions.

Current vs. Expected behavior

const path = new URL("./workers/a", import.meta.url);
const file = "a";

const workers = {
  // Works
  a: new Worker(new URL("./workers/a", import.meta.url), {
    type: "module",
    name: "worker struct > a",
  }),
  // Nextjs treats as static file, does not work
  b: new Worker(path, {
    type: "module",
    name: "worker struct > b",
  }),
  // Runtime exception
  c: new Worker(new URL(`./workers/${file}`, import.meta.url), {
    type: "module",
    name: "worker struct > c",
  }),
  // Works
  d: new Worker(new URL(`./workers/${"a"}`, import.meta.url), {
    type: "module",
    name: "worker struct > d",
  }),
  // Runtime exception
  e: getWorker("test", "a"),
};

I would expect at a bare minimum a, c, d, and e to work but would also like additional clarification on b regarding fully dynamic path values.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000
Binaries:
  Node: 18.18.2
  npm: 9.8.1
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 14.1.0
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

This is really more of a general "how should this work?" type of question / bug report but given there may actually be a bug hidden in here I decided to open an issue vs. a discussion thread. Some backstory, my team is working on some abstractions around web workers and we were running into some oddities around how Next (or Webpack presumably) handles certain scenarios. Included in the code to reproduce the issue are five scenarios.

src/playground/worker-struct.ts

I have deduced that b is likely due to the note here regarding using fully dynamic values for import statements. The really odd one is c vs. d since those have a partially static path with c having a dynamic value.

⚠️ Additional note, none of the example seem to work with next --turbo

msdrigg commented 2 months ago

I am also seeing this problem. Currently none of these work with next dev or next dev --turbo, but next build works fine using a