supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

`window is not defined` error in `_initialize()`

Open azzazzel opened this issue 1 year ago • 9 comments

Bug report

  • [x] I confirm this is a bug with Supabase, not with my own application.
  • [x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

This change https://github.com/supabase/auth-js/commit/9751b8029b4235a63dcb525e7ce7cc942c85daf5#diff-3522461172efd6058d6b8da62fc2d30d8b524d2b64894ea2c67218c52f7fdff5R310 seems to introduce a ReferenceError: window is not defined error when initializing auth server-side via createServerClient from @supabase/ssr

To Reproduce

The error occurred in a Nuxt.js app however it should be reproducible by

import { createServerClient } from "@supabase/ssr";

const client = createServerClient(url, key, options);
await client.auth.getSession();

in any SSR-based app.

System information

  • OS: macOS
  • Version of Node.js: v23.3.0
  • Version of supabase-js: 2.47.10
  • Version of @supabase/auth-js: 2.67.3,
  • Version of @supabase/ssr: 0.5.2,

azzazzel avatar Jan 14 '25 15:01 azzazzel

This is reproducible for me, in SvelteKit, but doesn't affect my app's behavior. aka, it does not crash the app, but if I go into the catch and change the code to where it logs the error, the error you mention shows up in my console. Is it affecting your app?

I believe the rest of that initialize code is only relevant for client-side.

j4crev avatar Jan 14 '25 22:01 j4crev

I'm not sure. I found it while debugging an issue with exchangeCodeForSession not being called on the server side, which causes a hydration mismatch warning. I can't confidently confirm this is the root cause but it prevents me from properly debugging the flow.

azzazzel avatar Jan 14 '25 23:01 azzazzel

@kangmingtay do you recall why const params = parseParametersFromURL(window.location.href) was used here, which will always throw on the server-side, instead of checking isBrowser() or even if (typeof window !== 'undefined') first?

I know isBrowser() checks for window and document for undefined, so I'm not sure if using that would somehow exclude some kind of environment where window exists but document doesn't (like React Native I believe??).

j4crev avatar Jan 14 '25 23:01 j4crev

I'm not sure. I found it while debugging an issue with exchangeCodeForSession not being called on the server side, which causes a hydration mismatch warning. I can't confidently confirm this is the root cause but it prevents me from properly debugging the flow.

Have you tried passing auth: { debug: true } for your server client's options?

j4crev avatar Jan 14 '25 23:01 j4crev

Have you tried passing auth: { debug: true } for your server client's options?

Yes, but that alone doesn't help. The hydration issue is not consistent. When reloading the page, about 50% of the time it works fine, and the other 50% doesn't. That's likely an error on my side, though.

azzazzel avatar Jan 15 '25 16:01 azzazzel

I'm not sure. I found it while debugging an issue with exchangeCodeForSession not being called on the server side, which causes a hydration mismatch warning. I can't confidently confirm this is the root cause but it prevents me from properly debugging the flow.

@azzazzel if you'd like, post on the Discord server and maybe someone can help with your original issue. I'll chime in if I can.

https://discord.gg/TmJfUCy5 if you're not already on there.

j4crev avatar Jan 15 '25 17:01 j4crev

It turned out the hydration issue was due to a missing await in a well-hidden place in my code.

While throwing this error on the server side doesn't break the app, I'd suggest "fixing" it anyway. It'll make debugging less confusing for people who are not super familiar with the codebase.

Thank you for your help @j4w8n!

azzazzel avatar Jan 15 '25 18:01 azzazzel

This issue has been automatically marked as stale because it has not had any activity for 6 months. It will be closed in 30 days if no further activity occurs.

If this is still relevant, please comment to keep it open.

github-actions[bot] avatar Apr 12 '26 00:04 github-actions[bot]

Closed due to inactivity. Please open a new issue if this is still relevant.

github-actions[bot] avatar May 17 '26 00:05 github-actions[bot]