When using useAuthenticator
build fails because of server-side rendering.
#5778
Labels
Authenticator
An issue or a feature-request for an Authenticator UI Component
question
General question
React
An issue or a feature-request for React platform
Before creating a new issue, please confirm:
On which framework/platform are you having an issue?
React
Which UI component?
Authenticator
How is your app built?
Next.js
What browsers are you seeing the problem on?
No response
Which region are you seeing the problem in?
No response
Please describe your bug.
I am building a Next.js project and my
_app.tsx
file looks like this:In this structure, I use the
<AccessManager>
component to handle user status in the whole project instead of doing this granularly per page. The code for this component is the following:The code works well when I run things locally, but the problem appears when I try to build the project (
npm run build
). I used to handle this logic without the@aws-amplify/ui-react
library and things worked; I used to have a separate context file that basically handles whatuseAuthenticator
does. The build command fails because the component is being server-side rendered during the build.The problem with doing SSR with this component is that I use the
next/router
library which only works on the client-side. The error message I get is the following and it appears for multiple pages:Error: No router instance found. you should only use "next/router" inside the client side of your app. https://nextjs.org/docs/messages/no-router-instance
.I already confirmed the build works if I remove the
useAuthenticator
from this file or if I remove thenext/router
and keep theuseAuthenticator
, however, both provide important functionality that I need to maintain. Any recommendations on what would be the best way to handle this scenario? Is what I'm doing with the<AccessManager>
component a good practice and if so how can I improve this to avoid the current error?What's the expected behaviour?
I was not expecting
useAuthenticator
to cause SSR issues.Help us reproduce the bug!
It should be reproducible if you try to run
npm run build
in a project that usesuseAuthenticator
inside a component that at the same time usesnext/router
. The component should be used in the_app.tsx
file.Code Snippet
With the help of Claude I already made it work using a
useEffect()
that avoids doing SSR on the whole code. However, I am not a big fan of this solution cause I think handling the client state can cause some jitter in the application when it loads for the first time. What would be the best way to handle what I am trying to do?Console log output
No response
Additional information and screenshots
No response
The text was updated successfully, but these errors were encountered: