Skip to main content
0 reactions

How to Refresh Server Components with router.refresh() in Next.js

A dark purple card titled router.refresh() in Next.js, labelled Next.js How-to, with a four-step flow: sign out in the browser, router.refresh(), the page renders again, not signed in.
router.refresh() in Next.js.

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

SyntaxTypeScript
const router = useRouter();

router.refresh();
  • useRouter comes from next/navigation and 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 (opens in a new tab) 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.tsxTypeScript
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.tsxTypeScript
"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:

A Status page that still says Signed in as Alex Rivera after the Sign out button was clicked.
Without router.refresh(), the old text stays.

Step 3: Add router.refresh()

Call router.refresh() after the sign-out:

app/status/sign-out-button.tsxTypeScript
"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”:

A Status page that says Not signed in, with a Sign out button.
With router.refresh(), the server renders the page again.

Common Mistakes

MistakeWhat happensDo this instead
Calling it from a server componentuseRouter only works in client componentsCall it in a client component, after the change
Calling it before the change has finishedThe server renders the old stateawait the change first
Reloading the whole page insteadThe browser loses its React state and scroll positionUse router.refresh()

Troubleshooting

ProblemCauseFix
The page still shows old data after refreshThe data comes from a server cache, which router.refresh() doesn’t clearUse revalidatePath or revalidateTag for cached data
An error about the router when you call ituseRouter was imported from next/router, the Pages Router versionImport 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

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.

0 comments