Next.js throws NEXT_REDIRECT and the redirect never happens
NEXT_REDIRECTShort answer
redirect() works by throwing a NEXT_REDIRECT error that Next.js catches itself. If your own try/catch catches it first, the redirect is swallowed and you see the error. Call redirect() outside the try block. Inside a client event handler use the useRouter hook instead.
Fix it fast
1Find out which cause you have
Find the redirect() call and check whether it sits inside a try block, or inside an onClick or onSubmit handler.
grep -rn "redirect(" app/ --include=*.ts --include=*.tsx2Match what you see, then jump to its fix
What does the error look like?
NEXT_REDIRECT
Cause 1 of 2
redirect() is called inside a try/catch
1Check
redirect() throws an error to stop rendering. A catch block around it receives that error before Next.js can handle it.
try {
await saveUser(data)
redirect("/dashboard") // caught below
} catch (e) {
console.error(e) // prints NEXT_REDIRECT
}2Fix
Do the risky work inside try/catch and call redirect() after it, outside the block.
let ok = false
try {
await saveUser(data)
ok = true
} catch (e) {
console.error(e)
}
if (ok) redirect("/dashboard")3Verify
Call the route again. The response should be a 307 with a Location header, and the catch block should stay silent.
curl -i -X POST http://localhost:3000/api/saveConfirmed by nextjs.org
Cause 2 of 2
redirect() is called from a client event handler
1Check
In a Client Component, redirect() is allowed while rendering but not inside an event handler such as onClick.
grep -rn "onClick\|onSubmit" app/ --include=*.tsx2Fix
Use the useRouter hook in the handler, or call a Server Action that runs redirect().
'use client'
import { useRouter } from 'next/navigation'
export function Go() {
const router = useRouter()
return <button onClick={() => router.push('/dashboard')}>Go</button>
}3Verify
Click the button and check the URL changes.
# open the page in the browser and click the buttonConfirmed by nextjs.org
How do I stop it coming back?
- Treat redirect() like return: it ends the function, so put it last.
- Keep client navigation in useRouter and server navigation in redirect().
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fix