#Router.push() not triggering a rerender for template.ts

1 messages · Page 1 of 1 (latest)

keen wren
#

import React from "react";
import { getServerSession } from "next-auth";
import Logout from "./components/Logout";
import { headers } from "next/headers";

type Props = {
children: React.ReactNode;
};

const template = async ({ children }: Props) => {
console.log();
const isAuth = headers().get("X-Authenticated-User");
const session = await getServerSession();
console.log();
if (isAuth) {
console.log("YES YES");
}
console.log("session from template", isAuth);
return (
<div>
<nav>{isAuth !== "null" ? <Logout /> : <div>Login</div>}</nav>
{children}
</div>
);
};

export default template;

when I login i do router.push("/admin/");
but its not changing the login to logout until i refresh the page

fresh sageBOT
#

Hi @keen wren. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it seems that you're experiencing a common issue with Next.js where router.push() doesn't trigger a complete page refresh, which includes re-rendering the template.ts file. This is because router.push() only navigates to a different route without refreshing the whole page.

In one of the Github issues (https://github.com/vercel/next.js/issues/54766), a user mentioned a similar problem where router.push() didn't refresh the layout. The user had to use router.refresh() after router.push() to refresh the layout. However, this approach is not ideal because router.push is not guaranteed to be done before router.refresh() kicks in, which could lead to refreshing the wrong page.

router.push(`/home`)
router.refresh(); // refresh the layout

The user suggested an enhancement to have an option like router.push('/', { refresh: true }) to tell Next.js to refresh the whole page including the layout for login/logout scenarios.

Unfortunately, based on the extracts, there's no direct solution provided for this issue. You might want to follow the progress on these issues or look for workarounds in the Next.js community.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@keen wren kapa.ai is still learning and improving, please let us know how it did by reacting below