DuCanhGH / next-pwa

Zero-config PWA plugin for Next.js.
https://ducanh-next-pwa.vercel.app
MIT License
554 stars 21 forks source link

[Bug - next-pwa]: `typeof window` transpiled as `object` at build time #156

Open JoseVSeb opened 2 weeks ago

JoseVSeb commented 2 weeks ago

Provide environment information

next@15.0.0-canary.35 @ducanh2912/next-pwa@10.2.7

Link to reproduction - Issues with a link to complete (but minimal) reproduction code help us address them faster

https://github.com/JoseVSeb/next-pwa-build-issue-typeof-window.git

To reproduce

  1. use typeof window in custom worker.
  2. run build.
  3. check the transpiled custom worker js file.

Describe the bug

typeof window is transpiled as object in service worker js.

Expected behavior

typeof window should be transpiled to undefined in service worker js.

Screenshots (if relevant)

No response

Additional information (if relevant)

I'm using an external library in my custom worker (firebase to control authentication) with typeof window in its code. when the application is built, the service worker fails to execute because the library tries to use window object.

this is caused because next build is configured to transpile typeof window as object in client side, but it should be undefined in service worker.

DuCanhGH commented 2 weeks ago

@JoseVSeb this is because same as Workbox, we create a child compiler on Next.js's web compiler (this allows us to inherit its webpack plugins and loaders, SWC configuration, etc.). As such, it is not really possible for us to change WebpackOptionsNormalized.target to "webworker" (this either doesn't have an impact on initiated plugins or have unexpected effects on them), and we have to inherit Next.js's webpack loaders and plugins, which may statically replace typeof window with "object". I suppose that we should replicate createChildCompiler's behaviour but with target changed. Not sure if that changes anything though. Perhaps I'll have to dive into Next.js's internals for a bit.

Edit: seems that Next.js does force SWC to not statically replace typeof window with "object". Not sure what's the cause, then. Ah, that seems to be for files in node_modules only, actually. In other cases, it is exactly Next.js's SWC configuration which causes this issue.

Edit 2: I'm looking into worker-loader. Doesn't seem to be so different from our current approach, though.

Edit 3: related issue: https://github.com/vercel/next.js/issues/61858

JoseVSeb commented 2 weeks ago

seems that Next.js does force SWC to not statically replace typeof window with "object". Not sure what's the cause, then. Ah, that seems to be for files in node_modules only, actually.

I'm currently overriding some internal packages of Firebase using pnpm workspace because of an unrelated bug in Firebase. So, if that weren't the case, I wouldn't be facing this issue then? (the issue is triggered from the internal package that I have overridden)

DuCanhGH commented 2 weeks ago

@JoseVSeb if the bug is related to typeof window, yes.