Next.js Discord

Discord Forum

useRouter().push("/") it not working in production build

Answered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
import { useRouter } from 'next/navigation'
const router = useRouter()
router.push('/') 

is not working
Answered by Samyar
I have something similar, and it worked for me.

Because it will re-render server components without losing client-side state so it will know that the user is authenticated on the server, and it has access to the provided page.
https://github.com/vercel/next.js/discussions/51782#discussioncomment-6470680
View full answer

9 Replies

West African LionOP
import { useRouter } from "next/navigation";
const router = useRouter();
router.push('/');
<motion.button
onTap={() => {
  console.log("router.push('/')")
    router.push("/");
}}
whileTap={{
    scale: 0.9,
    transition: { duration: 0.1, ease: "easeInOut" },
}}
whileHover={{
    scale: 1.1,
    transition: { duration: 0.2, ease: "easeInOut" },
}}
className={styles.LoginButton}
>
Login
</motion.button>

I made a plain button with nothing but just to test it whether its working or not but it still didnt work
What I am trying to achieve is on successful login i want to redirect user to homepage that is i want it to go from here
http://localhost:3000/login
to here
http://localhost:3000/
and it works on dev but not on build

Another weird thing,
Once i manually enter the URL after successful login it starts to work all of sudden


that is

I logged in, it didnt route me but login was successful so i entered the URL manually http://localhost:3000/ and when i click logout and try to login again it worked this time

It just doesnt work the first time if the page hasnt been visited yet
Here's the deployed one for u to check out
https://facedetection-two.vercel.app/
For Trying it out yourself
try these credentials
test@testing.com
123@passsword
I have something similar, and it worked for me.

Because it will re-render server components without losing client-side state so it will know that the user is authenticated on the server, and it has access to the provided page.
https://github.com/vercel/next.js/discussions/51782#discussioncomment-6470680
Answer