Next.js Discord

Discord Forum

typeof window, SSR issues

Unanswered
European hornet posted this in #help-forum
Open in Discord
European hornetOP
Hi guys, im trying to define a dynamic way to import my library if is SSR or client doing this loader

const isServer =  !process.browser; // typeof window === "undefined"
const sdk = isServer ?  await import('./ssr/sdk.js' ) : await import('./sdk.js')
export const { ...exports } = sdk;
export default sdk.default;


the weird case is that using typeof window === 'undefined' fails but with !process.browser works,

i understand that process.browser is legacy but for some reason typeof window keeps crashing my build, any ideas ?

this is to be able to use my library in both cases

17 Replies

Bombay
Hello @European hornet
Can we have a discussion?
European hornetOP
hey, yes, @Bombay
Bombay
I started running a startup with an unique idea
Are you from Brazil or Mexico?
European hornetOP
this is not the right place to talk about it
Bombay
Okay
Can we open DM ?
European hornetOP
sure
Gray-breasted Martin
What about using the dynamic function with ssr set to false?
European hornetOP
in my case i need to load both, to be able to do SSR rendering, @Gray-breasted Martin
Pacific sand lance
is there any reason split sdk into 2 files (i.e. imports that can't be bundled)?
European hornetOP
webpack in web mode always inject some document/window references
im really thinking to move to vite or other bundles, but i still researching
Pacific sand lance
you can check rspack, it has similiar config and can be used with non-browser projects too
it requires you to manually exclude native/builtin libs from being bundled tho, so if you are importing anything that shouldn't/can't be bundled - some issues may occur
European hornetOP
the issue is the flag himself, for the moment i had to move to vite and do a full SSR ESM build compatible with browser