Next.js Discord

Discord Forum

Next.js marks static pages as server rendered on demand (ƒ)

Answered
American Shorthair posted this in #help-forum
Open in Discord
Avatar
American ShorthairOP
I do not have 'use client' in any of my pages, some are only html elements and text
but it is not considered as ○ (Static) prerendered as static content, why?
Image
Answered by B33fb0n3
const cookie = cookies().get("refresh_token");
this will make the layout dynamically
View full answer

7 Replies

Avatar
looks like your layout is dynamically rendered making all pages dynamically rendered
Avatar
American ShorthairOP
I am getting userData in the layout

export default async function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  const userData = await getUserData();

  return (
    <html lang="en">
      <body>
        <JotaiProvider>
          <Navbar data={userData} />
          <section>{children}</section>
          <Footer />
        </JotaiProvider>
      </body>
    </html>
  );
}



import { cookies } from "next/headers";
import { jwtDecode } from "jwt-decode";
import { IJWTPayload, USER_ROLE } from "../types/global.types";

export async function getUserData() {
  const cookie = cookies().get("refresh_token");
  if (cookie) {
    const response = await fetch(
      `${process.env.SERVER_URL}/auth/refresh_token`,
      {
        headers: {
          Cookie: cookies().toString(),
        },
        next: { tags: ["auth"] },
      }
    );
    const data = await response.json();
    if (data.success) {
      const userData = jwtDecode<IJWTPayload>(data.payload);
      return { ...userData, token: data.payload };
    } else {
      return {
        id: "",
        role: [USER_ROLE.USER],
        avatar: "",
        token: "",
      };
    }
  } else {
    return {
      id: "",
      role: [USER_ROLE.USER],
      avatar: "",
      token: "",
    };
  }
}
Avatar
const cookie = cookies().get("refresh_token");
this will make the layout dynamically
Answer
Avatar
American ShorthairOP
I see, how do I resolve this. What do I do to make it static, any suggestions?
I also want to get the token if there is http only cookie, and then load user data
Avatar
you cannot get the token in a server component, because then it means all pages read the tokens in the server => dynamic rendering.

you can try client-side rendering for the navbar's auth state.
Avatar
American ShorthairOP
Alright thanks a lot