Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In web worker, typeof window === 'undefined' is falsely bundled to true #61858

Closed
yf-yang opened this issue Feb 9, 2024 · 11 comments · Fixed by #62051
Closed

In web worker, typeof window === 'undefined' is falsely bundled to true #61858

yf-yang opened this issue Feb 9, 2024 · 11 comments · Fixed by #62051
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked SWC Related to minification/transpilation in Next.js.

Comments

@yf-yang
Copy link

yf-yang commented Feb 9, 2024

Link to the code that reproduces this issue

https://github.com/yf-yang/next-webworker-bug

To Reproduce

  1. pnpm i
  2. pnpm dev
  3. open the window, click the button

Current vs. Expected behavior

Expected:
Web worker started, then debugjs can log a message "worker started" (console log level should be "verbose")
Current:
Error: Uncaught ReferenceError: window is not defined

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 22.3.0: Mon Jan 30 20:42:11 PST 2023; root:xnu-8792.81.3~2/RELEASE_X86_64
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: 8.14.3
Relevant Packages:
  next: 14.1.1-canary.45 // Latest available version is detected (14.1.1-canary.45).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

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

SWC transpilation

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

next dev (local)

Additional context

The error is this line in debug.js:
https://github.com/debug-js/debug/blob/f66cb2d9f729e1a592e72d3698e3b75329d75a25/src/browser.js#L119-L121

if (typeof window !== 'undefined' && window.process ...) {
  ...
}

In the bundled output file .next/static/chunks/_app-pages-browser_app_worker_js.js, this line is transpiled to

if (true && window.process ...) {
  ...
}

The bundled output is wrong, because in web worker, window does not exist.

NOTE: This bug does not exist in next@13.5.6

Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 22.3.0: Mon Jan 30 20:42:11 PST 2023; root:xnu-8792.81.3~2/RELEASE_X86_64
Binaries:
Node: 20.9.0
npm: 10.1.0
Yarn: 1.22.19
pnpm: 8.14.3
Relevant Packages:
next: 13.5.6
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: N/A
Next.js Config:
output: N/A

NEXT-2418

@yf-yang yf-yang added the bug Issue was opened via the bug report template. label Feb 9, 2024
@github-actions github-actions bot added the SWC Related to minification/transpilation in Next.js. label Feb 9, 2024
@yf-yang
Copy link
Author

yf-yang commented Feb 9, 2024

@kdy1 Is it a swc issue?

@kdy1
Copy link
Member

kdy1 commented Feb 9, 2024

No, it's not

@kdy1
Copy link
Member

kdy1 commented Feb 9, 2024

And this is duplicate

@yf-yang
Copy link
Author

yf-yang commented Feb 9, 2024

Ah, sorry for that.

#60644 (I think the issue may be incorrect because window should be undefined in web worker)

@yf-yang
Copy link
Author

yf-yang commented Feb 9, 2024

Also duplicates #61740
The same behavior: 14.0.5-canary.19 is OK, 14.0.5-canary.20 is broken.

I think both #60644 and #61740 does not address the issue correctly. window should be undefined, but typeof window !== 'undefined' should be false in a web worker context. (Or, does webpack create a window variable for workers?)

@yf-yang
Copy link
Author

yf-yang commented Feb 9, 2024

@huozhi Is it related to #59569?

@yf-yang
Copy link
Author

yf-yang commented Feb 9, 2024

I highly suspect #59569 is the root cause. isAppBrowserLayer does not imply this is the browser, it could be the web worker, where window object is not available. If I got it right, swc-loader is not configured correctly due to the problem.

See also: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#web_workers_api
The first paragraph.

@huozhi huozhi added the linear: next Confirmed issue that is tracked by the Next.js team. label Feb 9, 2024
@hiromaily
Copy link

hiromaily commented Feb 14, 2024

I'm using web worker and loaded worker code depends on other 3rd party library which checks window object.
It used to be working without any problems at Next.js version 14.0.4 or older. However it failed after upgrading next.js to 14.1.0. This error occurs both production and dev.

minimal.js:28 Uncaught ReferenceError: window is not defined
    at eval (webpack-internal:///(app-pages-browser)/./node_modules/protobufjs/src/util/minimal.js:28:68)

@huozhi
Copy link
Member

huozhi commented Feb 14, 2024

We should disable the swc optimizer for the node_modules in app browser layer. I filled a PR for fixing it

@yf-yang
Copy link
Author

yf-yang commented Feb 14, 2024

Thanks and happy new year :)

huozhi added a commit that referenced this issue Feb 14, 2024
)

### What

Disable swc transform optimizer for node_modules in browser layer of app
router bundles

Fixes #61858
Fixes #60644 
Fixes #60920
Fixes #61740
Closes NEXT-2418

### Why

In browser there could be not only one runtime, it could have both js
worker and browser. In js worker the `typeof window` is not as same as
in browser, so disabling the swc optimizer which will replace the code.
Leave the condition as it as.
huozhi added a commit that referenced this issue Feb 28, 2024
)

Disable swc transform optimizer for node_modules in browser layer of app
router bundles

Fixes #61858
Fixes #60644
Fixes #60920
Fixes #61740
Closes NEXT-2418

In browser there could be not only one runtime, it could have both js
worker and browser. In js worker the `typeof window` is not as same as
in browser, so disabling the swc optimizer which will replace the code.
Leave the condition as it as.
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked SWC Related to minification/transpilation in Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants