Next.js marks static pages as server rendered on demand (ƒ)
Answered
American Shorthair posted this in #help-forum
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?
but it is not considered as ○ (Static) prerendered as static content, why?
Answered by B33fb0n3
const cookie = cookies().get("refresh_token");this will make the layout dynamically
7 Replies
@American Shorthair 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?
looks like your layout is dynamically rendered making all pages dynamically rendered
@joulev looks like your layout is dynamically rendered making all pages dynamically rendered
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: "",
};
}
}
const cookie = cookies().get("refresh_token");this will make the layout dynamically
Answer
@B33fb0n3 > const cookie = cookies().get("refresh_token");
this will make the layout dynamically
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
@American Shorthair I also want to get the token if there is http only cookie, and then load user data
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.
you can try client-side rendering for the navbar's auth state.
American ShorthairOP
Alright thanks a lot