App directory weird bug where layout/page is cached and not dynamically rendering
Answered
Checkered Giant posted this in #help-forum
Checkered GiantOP
I use NextAuth and I have a layout which looks like this
However, the deposits/withdrawal pages under this route using this layout aren't working correctly.
What happens is, when a user loads the page and clicks "cashier" they are shown a login modal, however if the user logs in and clicks cashier, it doesn't detect their logged in and so still shows the login modal. However, if the user hard refreshes, they're not able to navigate to the page.
Does anyone know why this happens? It was my assumption that making a call to
export default async function CashierLayout({ children }: PropsWithChildren) {
const session = await getServerSession(authOptions);
if (session?.user && session?.auth) {
return (
<>
<div
className="container mx-auto justify-center p-5 max-w-[1280px]">
<CashierNavigation />
<hr className="w-1/2 mx-auto mb-4" />
{children}
</div>
</>
);
}
const urlSearchParams = new URLSearchParams({});
urlSearchParams.append("modal", "login");
await redirect(`/?${urlSearchParams}`);
}However, the deposits/withdrawal pages under this route using this layout aren't working correctly.
What happens is, when a user loads the page and clicks "cashier" they are shown a login modal, however if the user logs in and clicks cashier, it doesn't detect their logged in and so still shows the login modal. However, if the user hard refreshes, they're not able to navigate to the page.
Does anyone know why this happens? It was my assumption that making a call to
cookies() or headers() under next/headers would force this to be a dynamic call, could the issue be with route prefetching?Answered by Checkered Giant
yeah so disabling prefetching for those particular routes fixed the issue
18 Replies
Blanc de Hotot
I was having the same issue in my app for a while. I think I fixed it by changing
const session to let session, but you can always fall back on a good old-fashioned useEffect() if that doesn't work.Also, if you haven't wrapped your app in a SessionProvider, you should definitely do that too
Checkered GiantOP
cant useEffect in server components
Blanc de Hotot
Make it a client component
Checkered GiantOP
I can't it's a "layout"
I think it's because of prefetching anyway... going to make the change and see if it fixes the issue
just a hunch
Blanc de Hotot
As long as it's not the RootLayout, you can render as client if you need to
Checkered GiantOP
yeah but kinda don't wanna do that if im honest
Blanc de Hotot
Not good practice, I definitely agree
Checkered GiantOP
I'm only using client components if absolutely necessary
Blanc de Hotot
You can always abstract the functionality that requires the session or wrap the route in <Suspense /> pending a session
Checkered GiantOP
I'll let you know if its because of prefetching
@Blanc de Hotot You can always abstract the functionality that requires the session or wrap the route in <Suspense /> pending a session
Checkered GiantOP
thats actually not a bad idea...
if this doesnt work, I'm sure that will
thanks @Blanc de Hotot
Blanc de Hotot
Sure thing, best of luck! If it works you should post the solution back in here and mark it as the answer, I'm sure it'd help a ton of people
Checkered GiantOP
yeah so disabling prefetching for those particular routes fixed the issue
Answer