Next.js Discord

Discord Forum

Build bug

Answered
Yellow-horned horntail posted this in #help-forum
Open in Discord
Yellow-horned horntailOP
My dev environment has a minor bug from out of no where, but testing out my production build the error gets way worse. What freaking location??! Somebody please help me.:lolsob:
Answered by Ray
router.push() should be used inside the useEffect
View full answer

40 Replies

Yellow-horned horntailOP
Comes from next/navigation
Complete component/auth/index.tsx
@Yellow-horned horntail Complete `component/auth/index.tsx`
ok could you show the code on app/auth/forgot-pin/page.tsx?
@Yellow-horned horntail Complete `component/auth/index.tsx`
also check the import for useRouter in the code
Yellow-horned horntailOP
This is the same for all the other pages, I am scaffolding the app, so it's not like there are complex logic. The app is very fresh
make sure it is importing from next/navigation
forgot-pin.tsx
Yellow-horned horntailOP
that is react-native has nothing to do with the next app
ah
@Ray ah
Yellow-horned horntailOP
My initial reaction to the whole thing
then you should import userRouter from expo-router
Yellow-horned horntailOP
no no.... it is next app. yes both apps are sharing a codebase but the are very much INDEPENDENT of each other
I cd into web and start the app it has it's own package.json and everything
ok location is not defined error should be related to useRouter
could you show the code on /auth/set-new-pin?
@Ray ok location is not defined error should be related to useRouter
Yellow-horned horntailOP
I am importing it from next/navigation.
is your code on github which can be shared?
@Ray could you show the code on /auth/set-new-pin?
Yellow-horned horntailOP
EXACTLY the same as forgot-pin just a different names
@Ray is your code on github which can be shared?
Yellow-horned horntailOP
Sure give me a sec
I promise it has nothing to do with my code. but if it does I'd love to know where I erred.
Yellow-horned horntailOP
YAyy
What you got?
@Yellow-horned horntail What you got?
//components/auth/index.tsx
"use client";

import { useSelector } from "react-redux";
import { useRouter, usePathname } from "next/navigation";

import { authentication } from "@/redux/slices/auth";
import { useEffect } from "react";

function Auth({ children }: Readonly<{ children: React.ReactNode }>) {
  const { token } = useSelector(authentication);
  const router = useRouter();
  const pathname = usePathname();

  useEffect(() => {
    if (pathname === "/" && token === null) {
      router.push("/auth/sign-in");
    } else {
      router.push(pathname);
    }
  }, [pathname, token, router]);

  return <>{children}</>;
}

export default Auth;
router.push() should be used inside the useEffect
Answer
Yellow-horned horntailOP
Actually that absolutely crossed my mind. But I was like it works:lolsob:
@Yellow-horned horntail Actually that absolutely crossed my mind. But I was like it works<:lolsob:753870958489632819>
because it is using location which is a browser api and not available on the server
Yellow-horned horntailOP
Do you work at next Ray? :please_cat:
no lol
Yellow-horned horntailOP
Ha ha
But how is it using location
router.push() is using the location api under the hood
Yellow-horned horntailOP
I totally get why the build will fail. since it wraps every page from the layout when it builds each page it runs into the issue.
yeah
@Ray router.push() is using the location api under the hood
Yellow-horned horntailOP
Ahh I see. Thanks my dude! I was about to abandon next for the project.