Skip to content

fix: non browser environment compatibility which breaks SSR compilation#226

Open
khelkun wants to merge 2 commits intoyoannmoinet:masterfrom
3dverse:master
Open

fix: non browser environment compatibility which breaks SSR compilation#226
khelkun wants to merge 2 commits intoyoannmoinet:masterfrom
3dverse:master

Conversation

@khelkun
Copy link
Copy Markdown

@khelkun khelkun commented Jun 25, 2025

Hello @yoannmoinet ,

To make the story as short as possible:

  • super.js has global access to the global window object which only exists in a web browser environment.
  • I use nextjs, which uses SSR component (server side rendered component).
  • SSR component doesn't need nipplejs, but it imports client side components which use nipplejs.
  • That means SSR compilation imports the nipplejs module no matter it really uses it server side or not.
  • So the SSR compilation fails because the window object in super.js is accessed at the nipplejs module import, and also because on top of that the nipplejs library itself is built as umd but without defining globalObject in webpack config for the compatibility on non browser environments.

For the record, the error is:

ReferenceError: window is not defined

So this PR has 2 commits:

  • one to prevent accessing window object globally during the super.js module import
  • another to define globalObject in non browser environment and throw an error if nipplejs Manager.create is actually called from a non browser environment.

If you could accept that and release a nipplejs version toward it, I'd be really grateful 🙏

N.B: I first tried to mock the nipplejs module resolution during SSR compilation using the webpack config of nextjs config. But I did not find a config that fix the issue. Apparently because the nodejs resolve the modules earlier but I'm not sure about that...

khelkun added 2 commits June 26, 2025 10:35
…wser environments, and throw error if Manager.create is called in such environment
@khelkun khelkun marked this pull request as ready for review June 26, 2025 08:49
@khelkun khelkun changed the title fix: global access to window object breaks ssr compilation fix: non browser environment compatibility which breaks SSR compilation Jun 26, 2025
@yoannmoinet
Copy link
Copy Markdown
Owner

yoannmoinet commented Jun 26, 2025

Thanks for the very well detailed PR, really appreciated.
I'll have a look asap.

@yoannmoinet
Copy link
Copy Markdown
Owner

I'm wondering if we'd still have the issue with the massive v1 rewrite I'm currently doing in #221
Given that I'm now bundling the code differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants