vercel / vercel/next.js

Web workers do not work when runtime is edge

Open
#67,910 7 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Runtime Webpack
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/jauniusmentimeter/reproduction-app

To Reproduce
  1. Start the application with next dev command
  2. Open the page http://localhost:3000
  3. The error "document is not defined is returned from the server"
  4. Open app/page.tsx and change runtime to nodejs instead of edge. The error is not returned anymore.
Current vs. Expected behavior

Web workers do not work when runtime is set to edge. They should work in the same way as on nodejs runtime, since they are running on the client.

Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:14:38 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 20.11.0
  npm: 9.5.1
  Yarn: 1.22.21
  pnpm: 9.5.0
Relevant Packages:
  next: 15.0.0-canary.72 // Latest available version is detected (15.0.0-canary.72).
  eslint-config-next: N/A
  react: 19.0.0-rc.0
  react-dom: 19.0.0-rc.0
  typescript: 5.3.3
Next.js Config:
  output: N/A
Which area(s) are affected? (Select all that apply)

Runtime, Webpack

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

next dev (local), next build (local), next start (local), Vercel (Deployed)

Additional context

For some reason document is trying to be accessed on server environment when web worker code exist:
image
here is the place in .next/server/edge-runtime-webpack.js that is failing:
image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction app, run it with next dev, and compare app/page.tsx using the edge and nodejs runtimes. Inspect the generated .next/server/edge-runtime-webpack.js location shown in the report; done means the web worker runs under the edge runtime without the server-side document is not defined error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, react, typescript
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.