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

window is not defined error in _initialize() #1021

Open
2 tasks done
azzazzel opened this issue Jan 14, 2025 · 7 comments
Open
2 tasks done

window is not defined error in _initialize() #1021

azzazzel opened this issue Jan 14, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@azzazzel
Copy link

Bug report

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

Describe the bug

This change 9751b80#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 azzazzel added the bug Something isn't working label Jan 14, 2025
@azzazzel azzazzel changed the title ReferenceError: window is not defined error in _initialize() window is not defined error in _initialize() Jan 14, 2025
@j4w8n
Copy link
Contributor

j4w8n commented Jan 14, 2025

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.

@azzazzel
Copy link
Author

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.

@j4w8n
Copy link
Contributor

j4w8n commented Jan 14, 2025

@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??).

@j4w8n
Copy link
Contributor

j4w8n commented Jan 14, 2025

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?

@azzazzel
Copy link
Author

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.

@j4w8n
Copy link
Contributor

j4w8n commented Jan 15, 2025

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.

@azzazzel
Copy link
Author

azzazzel commented Jan 15, 2025

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants