Next.js Discord

Discord Forum

redirect("/") doesn't work in a server actions and keeps giving typescript errors

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
still just in a dev server, but as the title says, I cannot figure out why redirect isn't working. Works great in my other components but I'm trying to redirect after a Prisma create in a server action to show a success screen

this is what typescript says i need for the redirect:
redirect(res: NextApiResponse, statusOrUrl: string | number, url?: string | undefined): NextApiResponse<any>

this is what i've tried and what it tells me:
redirect("/animal-added")
typecript error:
Expected 2-3 arguments, but got 1.ts(2554)
index.d.ts(28, 56): An argument for 'statusOrUrl' was not provided.

the more i try and satisfy the typescript errors, the more that come up, when i try to put an argument for statusOrUrl, it gives me NextApiResponse type errors

I think im fundementally misunderstanding whats going wrong so anything to point me in the right direction will help.

7 Replies

Shy Albatross
it wants 2 or 3 arguments, you're giving it one
first is the response, 2nd is the url or status, if 2nd is status then 3rd is url
Transvaal lionOP
where do i get the response from of type NextApiResponse'
Shy Albatross
where did you import the redirect from?
probably page router, you need to import it from next/navigation if you're in App Router
Transvaal lionOP
import { redirect } from "next/dist/server/api-utils"
that was the issue, i was using the wrong redirect import, thx for your help!
importing from "next/navigation" is the solution