Next.js Discord

Discord Forum

Client error

Answered
Akita posted this in #help-forum
Open in Discord
AkitaOP
Hi! I have a problem. I have suspense put on children in the layout, and in page.tsx a check to see if it has access to see it. In case it doesn't have access, I give it redirect('/').
The problem is that in production I get client error when I have to give redirect, why?
Answered by Ray
try to keep the layout simple first
View full answer

91 Replies

AkitaOP
AkitaOP
page.tsx
layout.tsx
@Akita Click to see attachment
no error on dev?
maybe the error is coming from AdminPerms.hasAdminAccess()?
AkitaOP
No, bcs when i remove
suspense it works
and i got that error also for 404 page
oh how about remove the suspense and create a loading.tsx?
AkitaOP
i did it
or
do you get error with loading.tsx?
AkitaOP
yes
anything in the browser console?
AkitaOP
if i have a loading.tsx file i dont need to use suspense?
do you get error if you just call redirect there like this
export default function Page() {
  redirect("/?error=You do not have permission to access this page.");

  return ....
}
@Akita if i have a `loading.tsx` file i dont need to use suspense?
yes because loading.tsx do that for you
it is better to use loading.tsx because next will prefetch it
AkitaOP
i will try that
thx
no prob
AkitaOP
When I go to a 404 page and try to click on the "Home" link, it takes a very long time, and from what I see in the console, it somehow gives a POST on the 404 route before going to the first page.
@Ray
AkitaOP
middleware.ts with withAuth from next-auth
And when i try to go to a page with redirect('/')
@Akita Click to see attachment
AkitaOP
like it
i got this Error: Minified React error #310; visit https://reactjs.org/docs/error-decoder.html?invariant=310 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
@Akita middleware.ts with withAuth from next-auth
could you rename the middlware.ts to _middleware.ts and try again?
AkitaOP
yes, from 404 page
oh only in the 404 page right?
AkitaOP
i think so
<Link href='/'>Home</Link> like this?
AkitaOP
yes
@Akita Click to see attachment
this is the home page?
AkitaOP
no
That s the homepage
hard to tell what happen, is the project on github which can share?
AkitaOP
i cant share it 😦
@Akita Click to see attachment
it is odds to see this error when you try to navigate to home
AkitaOP
The main problem wouldn't be that with 404, the problem would be on pages where I have admin restriction.
There, when it wants to enter and I have suspense or loading.tsx, I get client error, then it redirects me.
When I don't have loading.tsx or suspense, it works without problem.
what error you get? do you see anything in browser console and server console?
can you show the code in AdminPerms.hasAdminAccess()?
have you tried it without executing AdminPerms.hasAdminAccess()?
AkitaOP
i tried it without loading.tsx and it works
export default async function Page() {
   const session = await getAuth()

   if (!session) redirect("/?....")

   return ...
}

like this?
and it only work without loading.tsx?
AkitaOP
Yes
it works without loading.tsx
export default async function Page() {
   redirect("/?....")

   return ...
}

does this work with loading.tsx?
AkitaOP
1 sec, i will create another page for test
ok
AkitaOP
Still not working
AkitaOP
import { redirect } from "next/navigation"

export default async function Page() {
    redirect('/')
    return <></>
}
can you try it with the provider comment out?
the authprovider maybe
AkitaOP
authprovider is just with sessionProvider from next-auth
try to keep the layout simple first
Answer
AkitaOP
1 sec, i removed uiprovider and authprovider
like commenting out InsertingItemsLoader ToasterComponent
ok
AkitaOP
thx
@Akita thx
oh nice, so which part cause the error?
AkitaOP
inserting new items component
ah ok
AkitaOP
it is client component, i need to check it with a state if isClient is true
that's make sense, that's why you made a POST request
AkitaOP
i made a hook
for that
thx
no prob
AkitaOP
404 page still not working
@Akita 404 page still not working
what is not working?
AkitaOP
i made a custom 404 page and it works, problem solved
thx again