All pages are dynamically rendered even when they don't have anything dynamic on the page itself
Answered
Riday π posted this in #help-forum
Every single page in my app is dynamic even though I am not using any dynamic function. Pages like about, terms, privacy, etc just have pure text content.
The only issue I could think of is that my header needs session to render the login/user button
The only issue I could think of is that my header needs session to render the login/user button
// src/app/layout.tsx
async function RootLayout() {
const session = await getSession();
return (
<Header isLoggedIn={!!session?.user} picture={session?.user?.picture} />
<main className='...'>{children}</main>
...
)
}40 Replies
most likely,
getSession() is using dynamic functionAnswer
@Ray where is `getSession()` from?
export const getSession = cache(async (required = false): Promise<AuthSession> => {
const sessionId = cookies().get(lucia.sessionCookieName)?.value;
if (!sessionId && required) redirect('/auth/login');
if (!sessionId) return null;
const result = await lucia.validateSession(sessionId);
// Next.js throws when you attempt to set cookie when rendering page.
try {
if (result.session && result.session.fresh) {
const { name, value, attributes } = lucia.createSessionCookie(result.session.id);
cookies().set(name, value, attributes);
}
if (!result.session) {
const { name, value, attributes } = lucia.createBlankSessionCookie();
cookies().set(name, value, attributes);
}
} catch {}
if (!result.session && required) redirect('/auth/login');
return { user: result.user, ...result.session };
});Uh
So, how do I make the pages static, but have the header dynamic?
you should create a route-group
(authenticated)/layout.tsx <-- use getSession() hereOh
@Riday π So, how do I make the pages static, but have the header dynamic?
partial prerendering
or client side rendering the header if you don't want to use experimental stuff
@Ray partial prerendering
Something like this?
<Suspense ... >
<GetSessionAndRenderHeader />
</Suspense>@Riday π Something like this?
tsx
<Suspense ... >
<GetSessionAndRenderHeader />
</Suspense>
https://nextjs.org/docs/app/api-reference/next-config-js/partial-prerendering
you need to enable it in
you need to enable it in
next.config and use next@canaryAHHH
I guess CSR it is
Does
revalidatePath() re-render the csr part too?Or, do I need to add another
revalidateTag()? (to revalidate the session fetched in header)how are you gonna do the csr part?
fetch the session from a route - the fetch will have a tag
it shouldn't be related to
revalidatePath or revalidateTagthe
tag nor the revalidate does nothing when fetching on clientOh
If I do
revalidatePath('/page'), will it re-render the client component and fetch again?client side fetching only happen when it mount
you don't have to worry about the caching for the csr part
just make sure the GET endpoint is dynamic which is static by default
@Riday π Is next-canary stable enough to use in client projects?
I can't answer that because Im not sure how your site build
or are you gonna provide the support if something wrong happen?
btw, even canary could be use but partical preredering is not suitable for production yet
Okay, nevermind then
@Ray `(authenticated)/layout.tsx` <-- use getSession() here
Can you explain more about this? How would I go about placing stuff here? I guess from what I understand, I place all the pages that need auth in that route group and everything else outside. Session will be fetched only in the auth group, but then I won't be able to show the header there?
create 2 version of header
one with session and one without session
export function Header() {
// render ui
return (<header></header>)
}
export async function HeaderWithSession() {
const session = await getSession()
return <Header />
}Oh, right. I forgot about this. I'll try something out