-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Gatsby build failing if you reference navigator at all during SSR: "navigator" is not available during server side rendering #19317
Comments
I just ran into this same problem. |
I find it kind of odd the build completely fails when there's checks like |
@kylegwalsh I did figure out that dynamically importing Amplify works; however, it gets a little weird because of how the module is built (aws-amplify/amplify-js#488). The main thing is configure is inside of default. I threw my import in useEffect (aka componentDidMount).
Hope that helps some. |
That's easy enough for initially configuring it, but I use amplify pretty extensively for custom authentication functions and would have to add a ton of conditions throughout my application. I'd rather not add a lot of additional logic if the solution is as easy as suppressing these errors during the gatsby build process. |
@kylegwalsh I'm in the beginning stages of the current app I'm working on and still trying to establish a good plan. One thing I was thinking of doing was creating an AuthProvider component to house all the function calls (signIn, signOut, etc) and state, then using the React useContext hook in any components that require it. This way, I'm not completely coupled to a library throughout my app if something better comes along or something like this very problem comes up. |
Oh... and, I know this isn't ideal at all to do, but you if go into the AWSPinpointProvider.js file in the amplify library and change...
to something like...
...this will alleviate your issue for the time being if it's a complete blocker for you. You may even be able to just set the var to false for now without any issues. I was trying to figure out how to get |
Hitting same problem as well |
@Bruqui I would have to fork amplify in order to make those changes to the pinpoint file because my build occurs during the CI process and the fresh library would just be installed. For now I guess I’ll look into a clean way to add the conditions without messying up the logic too much. |
@kylegwalsh Well, at least the dynamic import is an option to alleviate the issue. Again, not ideal having to go through a refactor because of it. Hopefully someone can figure out this issue of conditionals failing that shouldn't during Gatsby's build so issues like this stop popping up since Amplify isn't going to be the only issue with this. Any third-party library that uses things like window, document, navigator, etc. are probably going to fail during build process. |
Cross linking to aws-amplify issue that is related/same issue. |
@ramseytisher Awesome! Looks like this will fix our issue with amplify. I'm still a little concerned on the gatsby side because of the language used to describe the error though: This really sounds like gatsby is specifically throwing the error because navigator exists during SSR (even if it shouldn't cause any issues). Just something to think about in case other libraries result in similar issues. I could be wrong though! |
Thank you for opening this! This wasn't caused by a change of Gatsby's behavior but a PR in Amplify: They also already fixed this and will release it in the next minor, so look out for that. We're marking this issue as answered and closing it for now but please feel free to comment here if you would like to continue this discussion. We also recommend heading over to our communities if you have questions that are not bug reports or feature requests. We hope we managed to help and thank you for using Gatsby! |
This is perhaps tangential but I am getting this issue with the BBC peaks.js library. https://github.com/bbc/peaks.js/tree/master
What are the steps to alleviating the problem? If I was pointed in the right direction I could make a PR to the original library to fix the problem for myself and others. |
See gatsbyjs/gatsby#19317 - even attempting to reference "navigator" directly will throw an error. In passing fix whitespace issue in directory.json.
See gatsbyjs/gatsby#19317 - even attempting to reference "navigator" directly will throw an error. In passing fix whitespace issue in directory.json.
Description
It looks like the latest version of gatsby (2.17.7) is throwing errors during build time if you reference "navigator" at all (regardless of usage).
I have been using gatsby and aws-amplify together for a while now, but I recently started receiving build errors after upgrading gatsby.
Steps to reproduce
I would include a repository, but I would have to expose my AWS credentials in order for anyone to actually be able to run the code. I believe this issue is pretty straight forward and not aws-amplify specific, but please correct me if I'm wrong!
To reproduce this error:
yarn add gatsby aws-amplify
Auth
via aws-amplifyAuth
related methods somewhere in the applicationgatsby build
Expected result
Gatsby should allow the build to complete if "navigator" is being referenced merely to determine if it exists (as a boolean).
Actual result
Gatsby build fails and complains that
"navigator" is not available during server side rendering.
Here is the result of
gatsby build
:Amplify appears to just be checking whether navigator is available, which shouldn't cause problems during SSR.
I did not have any issues building on a previous version of gatsby (2.7.0). I understand why this check was probably added (to make sure you aren't trying to use something that isn't available during SSR), but this usage seems safe. I do not believe that this should be throwing an error.
Is there a way to suppress this error on my end, or is this a bug?
Environment
Additional Information
Versions:
I am only using the Auth portion of aws-amplify currently.
The text was updated successfully, but these errors were encountered: