Next.js Discord

Discord Forum

SSR question

Unanswered
Tonkinese posted this in #help-forum
Open in Discord
TonkineseOP
I have a layout file which I fetch my /me endpoint. I then have subsequent server pages which I call other endpoints.

Can I delay the call of other endpoints until the request in /me resolves successfully?

Even if I just return <div>hello</div> in my layout the requests from the main page.tsx still fires off >.>, any ideas appreciated

6 Replies

Sun bear
What exactly are you trying to achieve?
TonkineseOP
when I go to my homepage unauthed , I am fetching /me it returns 401, and my home page is fetching my users which also returns a 401

im trying to avoid the call to the users if the /me returned a 401
Sun bear
Try something like this out.

// /app/page.tsx
export async function HomePage() {
  const user = await getAuthUser();

  if (!user) {
    return <NotAuthenticatedHomePage  />
  }

  return <AuthenticatedHomePage/>
}


Then you can implement the 2 components <AuthenticatedHomePage /> which fetches the users and <NotAuthenticatedHomePage /> which displays the unauthed homepage. You can also use redirect function instead of returning NotAuthenticatedHomePage if your app requires authentication.

If your auth and not authed home pages don't differ much besides the component displaying users information you can also pass the user object to a component down below which conditionally renders based on it.

The example above is if your homepage highly differs based on auth value.
Sun bear
it works exactly the same in layout. share some code so we can see what you are doing wrong
you should only use auth context provider in order to access the user object freely on client side. auth checks should be done server side