Server components are rendered on the server. When something changes in the browser, such as signing out, the parts already on the page stay as they were. router.refresh() asks Next.js to render them again.
The short answer: in a client component, get the router with useRouter() from next/navigation, and call router.refresh() after the change.
Syntax
Syntax
const router = useRouter();
router.refresh();
useRoutercomes fromnext/navigationand works in client components.router.refresh()makes a new request to the server and renders the current page’s server components again. Next.js keeps the browser’s own state, such as React state and the scroll position.
See useRouter in the Next.js documentation.
Step 1: A Page with Server and Client Parts
The example is a small Next.js app with Better Auth. Its status page shows, from the server, who’s signed in. Create app/status/page.tsx:
app/status/page.tsx
import { headers } from "next/headers";
import { Suspense } from "react";
import { auth } from "@/lib/auth";
import { SignOutButton } from "./sign-out-button";
export default function StatusPage() {
return (
<main className="mx-auto w-full max-w-xl flex-1 px-6 py-12">
<h1 className="text-2xl font-semibold">Status</h1>
<Suspense fallback={<p className="mt-4">Loading…</p>}>
<SignedInAs />
</Suspense>
<SignOutButton />
</main>
);
}
async function SignedInAs() {
const session = await auth.api.getSession({
headers: await headers(),
});
return (
<p className="mt-4">
{session ? `Signed in as ${session.user.name}` : "Not signed in"}
</p>
);
}
Next to it, the client button, app/status/sign-out-button.tsx, signs out through Better Auth’s client:
app/status/sign-out-button.tsx
"use client";
import { authClient } from "@/lib/auth-client";
export function SignOutButton() {
async function handleClick() {
await authClient.signOut();
}
return (
<button
type="button"
onClick={handleClick}
className="mt-6 rounded border border-zinc-300 px-3 py-1"
>
Sign out
</button>
);
}
Step 2: See the Problem
Signed in as Alex, open http://localhost:3000/status and click Sign out. The sign-out works, but the server-rendered line doesn’t change:
Step 3: Add router.refresh()
Call router.refresh() after the sign-out:
app/status/sign-out-button.tsx
"use client";
import { useRouter } from "next/navigation";
import { authClient } from "@/lib/auth-client";
export function SignOutButton() {
const router = useRouter();
async function handleClick() {
await authClient.signOut();
router.refresh();
}
return (
<button
type="button"
onClick={handleClick}
className="mt-6 rounded border border-zinc-300 px-3 py-1"
>
Sign out
</button>
);
}
Sign in again, open the status page and click Sign out. Next.js renders the server part again, and it now says “Not signed in”:
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Calling it from a server component | useRouter only works in client components | Call it in a client component, after the change |
| Calling it before the change has finished | The server renders the old state | await the change first |
| Reloading the whole page instead | The browser loses its React state and scroll position | Use router.refresh() |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| The page still shows old data after refresh | The data comes from a server cache, which router.refresh() doesn’t clear | Use revalidatePath or revalidateTag for cached data |
| An error about the router when you call it | useRouter was imported from next/router, the Pages Router version | Import it from next/navigation |
FAQ
Does router.refresh() reload the page?
No. It fetches the server components again and merges them into the page, keeping the browser’s state.
When else is it useful?
After any change made in the browser that server components show, such as signing in, signing out or saving data through a client call.
Conclusion
When a change happens in the browser, call router.refresh() so the server components on the page show the new state.
Additional Resources
- useRouter in the Next.js documentation
- Basic usage in the Better Auth documentation: signing out
Related
Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.
