What is the difference between router.push and router.replace?

router.push adds a new entry to the browser history, so the Back button returns to the previous page. router.replace swaps the current entry, so the previous URL is gone from history.

Use push when

  • The user is moving to a new page they might want to come back from.
  • Navigating after creating something, such as going to /posts/123 after "Save".
  • Moving between steps where Back should return to the earlier step.

Use replace when

  • Updating filters or search query as the user types, so Back does not step through every keystroke.
  • Redirecting after login, so Back does not return to the login form.
  • Normalizing a URL, for example fixing casing or adding default query parameters.
  • Changing tabs that should not clutter history.

Shared behavior

  • Both perform a client-side transition, keeping shared layouts mounted.
  • Both accept { scroll: false } to keep the scroll position.
  • Both are wrapped in a React transition, so you can use useTransition to show pending UI.
  • <Link replace> is the declarative equivalent of router.replace.

Server side

redirect() in a Server Component or Server Function uses replace by default in Server Components and push in Server Functions. permanentRedirect() returns a 308 status for moved pages.

Code example

tsx
1// app/login/login-form.tsx
2"use client";
3
4import { useRouter } from "next/navigation";
5
6export function LoginForm() {
7 const router = useRouter();
8
9 async function onSubmit(formData: FormData) {
10 const res = await fetch("/api/login", { method: "POST", body: formData });
11 if (res.ok) {
12 // replace: Back should not return to the login form
13 router.replace("/dashboard");
14 }
15 }
16
17 return (
18 <form action={onSubmit}>
19 <input name="email" type="email" required />
20 <input name="password" type="password" required />
21 <button>Sign in</button>
22 </form>
23 );
24}
25
26// Elsewhere: push keeps history so Back works
27// router.push(`/posts/${newPost.id}`);
28// Declarative equivalent of replace:
29// <Link href="/settings/billing" replace>Billing</Link>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app